Grammalecte  Check-in [3834076ae7]

Overview
Comment:[build] helpers: use shutil and os module only
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | build | webext2
Files: files | file ages | folders
SHA3-256: 3834076ae7b4b20564f8b59639dc6e3486e9825623d2f106bfc152ab4ff4be7a
User & Date: olr on 2017-08-31 06:06:31
Other Links: branch diff | manifest | tags
Context
2017-09-01
19:16
[fx] useless HTML comment check-in: a7b45f2c3a user: olr tags: fx, webext2
2017-08-31
06:08
[build] merge commit on helpers check-in: 1182b20e51 user: olr tags: trunk, build
06:06
[build] helpers: use shutil and os module only check-in: 3834076ae7 user: olr tags: build, webext2
04:32
[fx] console.log adjustments check-in: 25bd31d7ed user: olr tags: fx, webext2
Changes

Modified helpers.py from [dc81791c7e] to [4468e2a847].

1
2
3

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Useful tools

import os

import zipfile

from distutils import dir_util, file_util
from string import Template


class cd:
    "Context manager for changing the current working directory"
    def __init__ (self, newPath):
        self.newPath = os.path.expanduser(newPath)

    def __enter__ (self):
        self.savedPath = os.getcwd()
        os.chdir(self.newPath)

    def __exit__ (self, etype, value, traceback):
        os.chdir(self.savedPath)


def unzip (spfZip, spDest, bCreatePath=False):
    "unzip file <spfZip> at <spfDest>"
    if spDest:
        if bCreatePath and not os.path.exists(spDest):
            dir_util.mkpath(spDest)
        print("> unzip in: "+ spDest)
        spInstall = os.path.abspath(spDest)
        if os.path.isdir(spInstall):
            eraseFolder(spInstall)
            with zipfile.ZipFile(spfZip) as hZip:
                hZip.extractall(spDest)
        else:
            print("# folder not found")
    else:
        print("path destination is empty")


def eraseFolder (sp):
    "erase content of a folder"
    # recursive!!!
    for sf in os.listdir(sp):
        spf = sp + "/" + sf

        if os.path.isdir(spf):
            eraseFolder(spf)
        else:
            try:
                os.remove(spf)
            except:
                print("%s not removed" % spf)


def createCleanFolder (sp):
    "make an empty folder or erase its content if not empty"
    if not os.path.exists(sp):
        dir_util.mkpath(sp)
    else:
        eraseFolder(sp)


def fileFile (spf, dVars):
    "return file <spf> as a text filed with variables from <dVars>"
    return Template(open(spf, "r", encoding="utf-8").read()).safe_substitute(dVars)



>


<




















|














<

|
>
|
|
|
<
|
|
|





|







1
2
3
4
5
6

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

42
43
44
45
46
47

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Useful tools

import os
import shutil
import zipfile


from string import Template


class cd:
    "Context manager for changing the current working directory"
    def __init__ (self, newPath):
        self.newPath = os.path.expanduser(newPath)

    def __enter__ (self):
        self.savedPath = os.getcwd()
        os.chdir(self.newPath)

    def __exit__ (self, etype, value, traceback):
        os.chdir(self.savedPath)


def unzip (spfZip, spDest, bCreatePath=False):
    "unzip file <spfZip> at <spfDest>"
    if spDest:
        if bCreatePath and not os.path.exists(spDest):
            os.makedirs(spDest, exist_ok=True)
        print("> unzip in: "+ spDest)
        spInstall = os.path.abspath(spDest)
        if os.path.isdir(spInstall):
            eraseFolder(spInstall)
            with zipfile.ZipFile(spfZip) as hZip:
                hZip.extractall(spDest)
        else:
            print("# folder not found")
    else:
        print("path destination is empty")


def eraseFolder (sp):
    "erase content of a folder"

    for sf in os.listdir(sp):
        spf = os.path.join(sp, sf)
        try:
            if os.path.isfile(spf):
                os.unlink(spf)
            elif os.path.isdir(spf):

                shutil.rmtree(spf)
        except Exception as e:
            print(e)


def createCleanFolder (sp):
    "make an empty folder or erase its content if not empty"
    if not os.path.exists(sp):
        os.makedirs(sp, exist_ok=True)
    else:
        eraseFolder(sp)


def fileFile (spf, dVars):
    "return file <spf> as a text filed with variables from <dVars>"
    return Template(open(spf, "r", encoding="utf-8").read()).safe_substitute(dVars)