forked from conda/conda
/
main_package.py
152 lines (119 loc) · 4.06 KB
/
main_package.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# (c) Continuum Analytics, Inc. / http://continuum.io
# All Rights Reserved
#
# conda is distributed under the terms of the BSD 3-clause license.
# Consult LICENSE.txt or http://opensource.org/licenses/BSD-3-Clause.
from __future__ import print_function, division, absolute_import
from conda.cli import common
descr = "Low-level conda package utility. (EXPERIMENTAL)"
def configure_parser(sub_parsers):
p = sub_parsers.add_parser('package', description=descr, help=descr)
common.add_parser_prefix(p)
p.add_argument(
'-w', "--which",
metavar = "PATH",
nargs = '+',
action = "store",
help = "given some PATH print which conda package the file came from",
)
p.add_argument(
'-L', "--ls-files",
metavar = 'PKG-NAME',
action = "store",
help = "list all files belonging to specified package",
)
p.add_argument(
'-r', "--reset",
action = "store_true",
help = "remove all untracked files and exit",
)
p.add_argument(
'-u', "--untracked",
action = "store_true",
help = "display all untracked files and exit",
)
p.add_argument(
"--pkg-name",
action = "store",
default = "unknown",
help = "package name of the created package",
)
p.add_argument(
"--pkg-version",
action = "store",
default = "0.0",
help = "package version of the created package",
)
p.add_argument(
"--pkg-build",
action = "store",
default = 0,
help = "package build number of the created package",
)
p.set_defaults(func=execute)
def list_package_files(pkg_name=None):
import os
import re
import conda.config as config
from conda.misc import walk_prefix
pkgs_dirs = config.pkgs_dirs[0]
all_dir_names = []
pattern = re.compile(pkg_name, re.I)
print('\nINFO: The location for available packages: %s' % (pkgs_dirs))
for dir in os.listdir(pkgs_dirs):
ignore_dirs = [ '_cache-0.0-x0', 'cache' ]
if dir in ignore_dirs:
continue
if not os.path.isfile(pkgs_dirs+"/"+dir):
match = pattern.match(dir)
if match:
all_dir_names.append(dir)
num_of_all_dir_names = len(all_dir_names)
dir_num_width = len(str(num_of_all_dir_names))
if num_of_all_dir_names == 0:
print("\n\tWARN: There is NO '%s' package.\n" % (pkg_name))
return 1
elif num_of_all_dir_names >= 2:
print("\n\tWARN: Ambiguous package name ('%s'), choose one name from below list:\n" % (pkg_name))
num = 0
for dir in all_dir_names:
num += 1
print("\t[ {num:>{width}} / {total} ]: {dir}".format(num=num, width=dir_num_width, total=num_of_all_dir_names, dir=dir))
print("")
return 1
full_pkg_name = all_dir_names[0]
print("INFO: All files belonging to '%s' package:\n" % (full_pkg_name))
pkg_dir = pkgs_dirs+"/"+full_pkg_name
ret = walk_prefix(pkg_dir, ignore_predefined_files=False)
for item in ret:
print(pkg_dir+"/"+item)
def execute(args, parser):
import sys
from conda.misc import untracked
from conda.packup import make_tarbz2, remove
prefix = common.get_prefix(args)
if args.which:
from conda.misc import which_package
for path in args.which:
for dist in which_package(path):
print('%-50s %s' % (path, dist))
return
if args.ls_files:
if list_package_files(args.ls_files) == 1:
sys.exit(1)
else:
return
print('# prefix:', prefix)
if args.reset:
remove(prefix, untracked(prefix))
return
if args.untracked:
files = sorted(untracked(prefix))
print('# untracked files: %d' % len(files))
for fn in files:
print(fn)
return
make_tarbz2(prefix,
name = args.pkg_name.lower(),
version = args.pkg_version,
build_number = int(args.pkg_build))