-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
109 lines (86 loc) · 3.33 KB
/
build.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
import json
import os
import sys
import shutil
import fnmatch
import functools
import itertools
import subprocess
# define additional patterns for files here that are not in the .gitignore file and should be omitted from the zipped mod
additionalIgnorePatterns = ['*.py', '*.exe', '.git', '.gitignore', '*.mp4', '.vscode', 'art']
# read all git ignore patterns that apply to a given git repo root directory
def readAllGitIgnorePatterns(dir):
# the repo's local .gitignore file
patterns = readGitIgnorePatterns(os.path.join(dir, '.gitignore'))
# the user's global excludesfile, most commonly ~/.gitignore
try:
gitExcludesFile = os.path.expanduser(
subprocess.check_output([
'git',
'--git-dir',
os.path.join(dir, '.git'),
'config',
'core.excludesfile'
]).rstrip().decode("utf-8")
)
patterns |= readGitIgnorePatterns(gitExcludesFile)
except subprocess.CalledProcessError:
pass
return patterns
# reads non-commented ignore patterns from a single .gitignore file
def readGitIgnorePatterns(gitIgnoreFilename):
patterns = set()
if os.path.isfile(gitIgnoreFilename):
with open(gitIgnoreFilename) as gitIgnoreFile:
for line in gitIgnoreFile:
if not line.startswith('#'):
if line.endswith('\n'):
patterns.add(line[:-1])
else:
patterns.add(line)
return patterns
# taken from http://stackoverflow.com/questions/5351766/use-fnmatch-filter-to-filter-files-by-more-than-one-possible-file-extension/25413436#25413436
# modified for ignoreFunction use
def find_files(dir_path: str=None, patterns: [str]=None) -> [str]:
path = dir_path or "."
path_patterns = patterns or []
file_names = os.listdir(path)
filter_partial = functools.partial(fnmatch.filter, file_names)
for file_name in itertools.chain(*map(filter_partial, path_patterns)):
yield file_name
# function to be passed to copytree, see https://docs.python.org/2/library/shutil.html#shutil.copytree
def ignoreFunction(directory, files):
#TODO curry this function to include modDir, rather than using global variable
ignorePatterns = readAllGitIgnorePatterns(modDir)
ignorePatterns = list(ignorePatterns)
for additionalPattern in additionalIgnorePatterns:
ignorePatterns.append(additionalPattern)
ignoredFiles = [file for file in find_files(directory, ignorePatterns)]
return ignoredFiles
## script start
# optionally read target directory from first command line parameter
if len(sys.argv) == 2:
modDir = sys.argv[1]
else:
modDir = '.'
if os.path.isdir(modDir):
modInfoJson = os.path.join(modDir, 'info.json')
if os.path.isfile(modInfoJson):
with open(modInfoJson) as infoFile:
modInfo = json.load(infoFile)
if modInfo is not None:
modName = modInfo['name']
modVersion = modInfo['version']
modFileName = modName + '_' + modVersion
tempDir = os.path.join ('.', 'tmp')
tempModDir = os.path.join(tempDir, modFileName)
shutil.copytree(modDir, tempModDir, ignore=ignoreFunction)
if os.path.isfile(modFileName + '.zip'):
os.remove(modFileName + '.zip')
shutil.make_archive(modFileName, 'zip', tempDir)
shutil.rmtree(tempDir)
else:
print('"%s" does not contain a "info.json" file.' %modDir)
else:
print('"%s" is not a valid directory.' % modDir)