Bladeren bron

close #3: refactored common functions down to utilities module

bmallred 11 jaren geleden
bovenliggende
commit
48b16c5296
10 gewijzigde bestanden met toevoegingen van 61 en 132 verwijderingen
  1. 4 12
      extensions/add.py
  2. 2 19
      extensions/clone.py
  3. 8 25
      extensions/incoming.py
  4. 4 12
      extensions/init.py
  5. 4 12
      extensions/merge.py
  6. 4 12
      extensions/mv.py
  7. 4 12
      extensions/pull.py
  8. 4 12
      extensions/push.py
  9. 4 12
      extensions/rm.py
  10. 23 4
      utilities.py

+ 4 - 12
extensions/add.py

1
import subprocess
2
from utilities import isGit, isMercurial, isBazaar
1
from utilities import execute, isGit, isMercurial, isBazaar
3
2
4
def add(arguments):
3
def add(arguments):
5
    '''
4
    '''
14
13
15
        command = ["git", "add"]
14
        command = ["git", "add"]
16
        command.extend(arguments)
15
        command.extend(arguments)
17
        executeCommand(command)
16
        execute(command)
18
17
19
    if isMercurial():
18
    if isMercurial():
20
        command = ["hg", "add"]
19
        command = ["hg", "add"]
21
        command.extend(arguments)
20
        command.extend(arguments)
22
        executeCommand(command)
21
        execute(command)
23
22
24
    if isBazaar():
23
    if isBazaar():
25
        command = ["bzr", "add"]
24
        command = ["bzr", "add"]
26
        command.extend(arguments)
25
        command.extend(arguments)
27
        executeCommand(command)
28
29
def executeCommand(command):
30
    '''
31
    Execute the given command.
32
    '''
33
34
    subprocess.call(command)
26
        execute(command)

+ 2 - 19
extensions/clone.py

1
import os
2
import subprocess
3
from utilities import isGit, isMercurial, isBazaar
1
from utilities import executeAndReturnResponse
4
2
5
def clone(arguments):
3
def clone(arguments):
6
    '''
4
    '''
16
    for commandSet in commandSets:
14
    for commandSet in commandSets:
17
        command = commandSet
15
        command = commandSet
18
        command.extend(arguments)
16
        command.extend(arguments)
19
        out, err = executeCommandWithResponse(command)
17
        out, err = executeAndReturnResponse(command)
20
18
21
        if err:
19
        if err:
22
            print("{0}: repository not found".format(command[0]))
20
            print("{0}: repository not found".format(command[0]))
23
        else:
21
        else:
24
            print("{0}: repository cloned!".format(command[0]))
22
            print("{0}: repository cloned!".format(command[0]))
25
            break
23
            break
26
27
def executeCommand(command):
28
    '''
29
    Execute the given command.
30
    '''
31
32
    subprocess.call(command, stderr=subprocess.STDOUT)
33
34
def executeCommandWithResponse(command):
35
    '''
36
    Execute the given command and return the output and errors.
37
    '''
38
39
    proc = subprocess.Popen(command, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
40
    return proc.communicate()

+ 8 - 25
extensions/incoming.py

1
import os
2
import subprocess
3
from utilities import isGit, isMercurial, isBazaar
1
from utilities import execute, executeAndReturnResponse, isGit, isMercurial, isBazaar
4
2
5
def incoming(arguments):
3
def incoming(arguments):
6
    '''
4
    '''
8
    '''
6
    '''
9
    
7
    
10
    if isGit():
8
    if isGit():
11
        out, err = executeCommandWithResponse(["git", "remote"])
9
        out, err = executeAndReturnResponse(["git", "remote"])
12
        if not err and out:
10
        if not err and out:
13
            executeCommandWithResponse(["git", "remote", "update", "-p"])
14
            executeCommand(["git", "log", "..@{u}"])
11
            executeAndReturnResponse(["git", "remote", "update", "-p"])
12
            execute(["git", "log", "..@{u}"])
15
13
16
    if isMercurial():
14
    if isMercurial():
17
        out, err = executeCommandWithResponse(["git", "paths"])
15
        out, err = executeAndReturnResponse(["git", "paths"])
18
        if not err and out:
16
        if not err and out:
19
            executeCommand(["hg", "incoming"])
17
            execute(["hg", "incoming"])
20
18
21
    if isBazaar():
19
    if isBazaar():
22
        out, err = executeCommandWithResponse(["bzr", "missing"])
20
        out, err = executeAndReturnResponse(["bzr", "missing"])
23
        if not err and out:
21
        if not err and out:
24
            executeCommand(["bzr", "missing"])
25
26
def executeCommand(command):
27
    '''
28
    Execute the given command.
29
    '''
30
31
    subprocess.call(command, stderr=subprocess.STDOUT)
32
33
def executeCommandWithResponse(command):
34
    '''
35
    Execute the given command and return the output and errors.
36
    '''
37
38
    proc = subprocess.Popen(command, stderr=open(os.devnull, 'w'), stdout=subprocess.PIPE)
39
    return proc.communicate()
22
            execute(["bzr", "missing"])

+ 4 - 12
extensions/init.py

1
import subprocess
2
from utilities import isGit, isMercurial, isBazaar
1
from utilities import execute, isGit, isMercurial, isBazaar
3
2
4
def init(arguments):
3
def init(arguments):
5
    '''
4
    '''
7
    '''
6
    '''
8
    
7
    
9
    if "git" in arguments or isGit():
8
    if "git" in arguments or isGit():
10
        executeCommand(["git", "init"])
9
        execute(["git", "init"])
11
10
12
    if "hg" in arguments or isMercurial():
11
    if "hg" in arguments or isMercurial():
13
        executeCommand(["hg", "init"])
12
        execute(["hg", "init"])
14
13
15
    if "bzr" in arguments or isBazaar():
14
    if "bzr" in arguments or isBazaar():
16
        executeCommand(["bzr", "init"])
17
18
def executeCommand(command):
19
    '''
20
    Execute the given command.
21
    '''
22
23
    subprocess.call(command)
15
        execute(["bzr", "init"])

+ 4 - 12
extensions/merge.py

1
import subprocess
2
from utilities import isGit, isMercurial, isBazaar
1
from utilities import execute, isGit, isMercurial, isBazaar
3
2
4
def merge(arguments):
3
def merge(arguments):
5
    '''
4
    '''
9
    if isGit():
8
    if isGit():
10
        command = ["git", "merge"]
9
        command = ["git", "merge"]
11
        command.extend(arguments)
10
        command.extend(arguments)
12
        executeCommand(command)
11
        execute(command)
13
12
14
    if isMercurial():
13
    if isMercurial():
15
        command = ["hg", "merge"]
14
        command = ["hg", "merge"]
16
        command.extend(arguments)
15
        command.extend(arguments)
17
        executeCommand(command)
16
        execute(command)
18
17
19
    if isBazaar():
18
    if isBazaar():
20
        command = ["bzr", "merge"]
19
        command = ["bzr", "merge"]
21
        command.extend(arguments)
20
        command.extend(arguments)
22
        executeCommand(command)
23
24
def executeCommand(command):
25
    '''
26
    Execute the given command.
27
    '''
28
29
    subprocess.call(command)
21
        execute(command)

+ 4 - 12
extensions/mv.py

1
import subprocess
2
from utilities import isGit, isMercurial, isBazaar
1
from utilities import execute, isGit, isMercurial, isBazaar
3
2
4
def mv(arguments):
3
def mv(arguments):
5
    '''
4
    '''
9
    if isGit():
8
    if isGit():
10
        command = ["git", "mv"]
9
        command = ["git", "mv"]
11
        command.extend(arguments)
10
        command.extend(arguments)
12
        executeCommand(command)
11
        execute(command)
13
12
14
    if isMercurial():
13
    if isMercurial():
15
        command = ["hg", "mv"]
14
        command = ["hg", "mv"]
16
        command.extend(arguments)
15
        command.extend(arguments)
17
        executeCommand(command)
16
        execute(command)
18
17
19
    if isBazaar():
18
    if isBazaar():
20
        command = ["bzr", "mv"]
19
        command = ["bzr", "mv"]
21
        command.extend(arguments)
20
        command.extend(arguments)
22
        executeCommand(command)
23
24
def executeCommand(command):
25
    '''
26
    Execute the given command.
27
    '''
28
29
    subprocess.call(command)
21
        execute(command)

+ 4 - 12
extensions/pull.py

1
import subprocess
2
from utilities import isGit, isMercurial, isBazaar
1
from utilities import execute, isGit, isMercurial, isBazaar
3
2
4
def pull(arguments):
3
def pull(arguments):
5
    '''
4
    '''
9
    if isGit():
8
    if isGit():
10
        command = ["git", "fetch"]
9
        command = ["git", "fetch"]
11
        command.extend(arguments)
10
        command.extend(arguments)
12
        executeCommand(command)
11
        execute(command)
13
12
14
    if isMercurial():
13
    if isMercurial():
15
        command = ["hg", "pull"]
14
        command = ["hg", "pull"]
16
        command.extend(arguments)
15
        command.extend(arguments)
17
        executeCommand(command)
16
        execute(command)
18
17
19
    if isBazaar():
18
    if isBazaar():
20
        command = ["bzr", "pull"]
19
        command = ["bzr", "pull"]
21
        command.extend(arguments)
20
        command.extend(arguments)
22
        executeCommand(command)
23
24
def executeCommand(command):
25
    '''
26
    Execute the given command.
27
    '''
28
29
    subprocess.call(command)
21
        execute(command)

+ 4 - 12
extensions/push.py

1
import subprocess
2
from utilities import isGit, isMercurial, isBazaar
1
from utilities import execute, isGit, isMercurial, isBazaar
3
2
4
def push(arguments):
3
def push(arguments):
5
    '''
4
    '''
9
    if isGit():
8
    if isGit():
10
        command = ["git", "push"]
9
        command = ["git", "push"]
11
        command.extend(arguments)
10
        command.extend(arguments)
12
        executeCommand(command)
11
        execute(command)
13
12
14
    if isMercurial():
13
    if isMercurial():
15
        command = ["hg", "push"]
14
        command = ["hg", "push"]
16
        command.extend(arguments)
15
        command.extend(arguments)
17
        executeCommand(command)
16
        execute(command)
18
17
19
    if isBazaar():
18
    if isBazaar():
20
        command = ["bzr", "push"]
19
        command = ["bzr", "push"]
21
        command.extend(arguments)
20
        command.extend(arguments)
22
        executeCommand(command)
23
24
def executeCommand(command):
25
    '''
26
    Execute the given command.
27
    '''
28
29
    subprocess.call(command)
21
        execute(command)

+ 4 - 12
extensions/rm.py

1
import subprocess
2
from utilities import isGit, isMercurial, isBazaar
1
from utilities import execute, isGit, isMercurial, isBazaar
3
2
4
def rm(arguments):
3
def rm(arguments):
5
    '''
4
    '''
9
    if isGit():
8
    if isGit():
10
        command = ["git", "rm"]
9
        command = ["git", "rm"]
11
        command.extend(arguments)
10
        command.extend(arguments)
12
        executeCommand(command)
11
        execute(command)
13
12
14
    if isMercurial():
13
    if isMercurial():
15
        command = ["hg", "rm"]
14
        command = ["hg", "rm"]
16
        command.extend(arguments)
15
        command.extend(arguments)
17
        executeCommand(command)
16
        execute(command)
18
17
19
    if isBazaar():
18
    if isBazaar():
20
        command = ["bzr", "rm"]
19
        command = ["bzr", "rm"]
21
        command.extend(arguments)
20
        command.extend(arguments)
22
        executeCommand(command)
23
24
def executeCommand(command):
25
    '''
26
    Execute the given command.
27
    '''
28
29
    subprocess.call(command)
21
        execute(command)

+ 23 - 4
utilities.py

6
    Determine if the current directory is part of a Git repository.
6
    Determine if the current directory is part of a Git repository.
7
    '''
7
    '''
8
8
9
    return testRepository(["git", "branch"])
9
    return executeCheckingForErrors(["git", "branch"])
10
10
11
def isMercurial():
11
def isMercurial():
12
    '''
12
    '''
13
    Determine if the current directory is part of a Mercurial repository.
13
    Determine if the current directory is part of a Mercurial repository.
14
    '''
14
    '''
15
15
16
    return testRepository(["hg", "branch"])
16
    return executeCheckingForErrors(["hg", "branch"])
17
17
18
def isBazaar():
18
def isBazaar():
19
    '''
19
    '''
20
    Determine if the current directory is part of the Bazaar repository.
20
    Determine if the current directory is part of the Bazaar repository.
21
    '''
21
    '''
22
22
23
    return testRepository(["bzr", "root"])
23
    return executeCheckingForErrors(["bzr", "root"])
24
25
def execute(command):
26
    '''
27
    Execute the given command.
28
    '''
29
30
    subprocess.call(command, stderr=subprocess.STDOUT)
31
32
def executeCheckingForErrors(command):
33
    '''
34
    Execute the given command and return whether any errors were found.
35
    '''
24
36
25
def testRepository(command):
26
    return subprocess.call(command, stderr=subprocess.STDOUT, stdout=open(os.devnull, 'w')) == 0
37
    return subprocess.call(command, stderr=subprocess.STDOUT, stdout=open(os.devnull, 'w')) == 0
38
39
def executeAndReturnResponse(command):
40
    '''
41
    Execute the given command and return the output and errors.
42
    '''
43
44
    proc = subprocess.Popen(command, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
45
    return proc.communicate()