-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopm.py
995 lines (769 loc) · 35.6 KB
/
opm.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
#python
#encoding=utf-8
import sys
import os
import os.path
import shutil
import urllib2
import hashlib
import re
from urlparse import urlparse, urljoin
from xml.etree import ElementTree
from filelistener import FileListener
import csscompiler
from csscompiler import CSSCompiler
DEBUG = False
CONFIG_FILENAME = 'template-config.xml'
SOURCE_FILENAME = '.template-info/source'
INFO_PATH = '.template-info/fileinfo'
PACKAGES_FILENAME = '.packages'
PACKAGE_FILENAME = '.package'
def relativeURI(url1, url2):
u''' url2相对于url1的相对路径字符串 '''
import re
# 去掉 ./ 这种表示当前目录的,学习一下 (?<=) 这种前导匹配
url1 = re.sub(r'(^\./|(?<=/)\./)', lambda m: '', url1)
url2 = re.sub(r'(^\./|(?<=/)\./)', lambda m: '', url2)
url1 = url1.split('/')
url2 = url2.split('/')
pos = 0
for i, part in enumerate(url1):
if url2[i] != part: break
else: pos = i
url2 = url2[i:]
url = len(url1[i+1:]) * '../' + '/'.join(url2)
return url
def path2uri(path):
u''' 将windows的path转换成没有协议的uri '''
return urlparse(path.replace('\\', '/'))[2]
class PackageExistsException(Exception):
def __init__(self, root_path):
self.root = root_path
class NotInWorkspaceException(Exception):
pass
class PackageNotFoundException(Exception):
def __init__(self, package_url, caller_root = None):
self.url = package_url
self.caller = caller_root
class WorkspaceNotFoundException(Exception):
pass
class ConfigError(Exception):
def __init__(self, path):
self.path = path
class File():
def __init__(self, path):
self.path = path
def read(self):
return open(self.path, 'rb').read()
class ModuleFile(File):
text_exts = ['.mustache', '.tpl', '.html']
json_exts = ['.json']
def __init__(self, path, base):
self.path = path
self.base = base;
def read(self):
module = path2uri(self.path[self.path.index('node_modules') + 13:])
if os.path.splitext(self.path)[1] in self.text_exts:
txt = open(self.path, 'rb').read()
txt = 'return "%s"' % txt.replace('"', '\\"').replace('\n', '\\\n');
elif os.path.splitext(self.path)[1] in self.json_exts:
txt = open(self.path, 'rb').read()
txt = 'return %s' % txt
else:
txt = open(self.path, 'rb').read()
id = urljoin(self.base, module)
dependencies = []
matches = re.finditer(r'require\([\'\"](.*)[\'\"]\)', txt, re.M | re.VERBOSE)
for match in matches:
dependencies.append(match.group(1))
dependencies = ', '.join(dependencies)
if dependencies:
dependencies = '\'' + dependencies + '\', '
txt = ';object.define(\'%s\', %sfunction(require, exports, module) {\n%s\n});\n' % (id, dependencies, txt)
return txt
class StaticPackage():
u''' 静态编译库 '''
def __init__(self, root_path, publish_path = None, workspace = None, listener = None):
self.url = None
self.combines = {}
self.listener = listener
self.workspace = workspace
self.root = root_path # 框架所在本地目录
self.publish_path = publish_path # 发布文件的目录路径
self.source_path = '' # 源文件的目录路径
self.resource_path = '' # 资源文件目录路径
self.library_path = '' # 库文件目录路径
self.library_folders = {}
self.combine_cache = {self.root: self} # 存储在combine过程中生成的package
self.resource_dir = None
self.serverPrefix = ''
self.serverUrl = ''
self.serverRoot = ''
self.load_config()
if not self.workspace: self.load_workspace()
if not self.listener: self.init_listener()
def load_workspace(self):
workspace_path = Workspace.get_workspace(self.root)
if workspace_path:
self.workspace = Workspace(workspace_path)
def init_listener(self):
# 不是所有的库都需要有publish_path
if self.publish_path:
self.listener = FileListener(os.path.join(self.publish_path, INFO_PATH))
def get_package(self, root_path):
u''' 从缓存中获取package引用,如果没有则生成新的并加入缓存 '''
package = self.combine_cache.get(root_path)
if not package:
package = StaticPackage(root_path, self.publish_path, workspace = self.workspace, listener = self.listener)
self.combine_cache[root_path] = package
package.combine_cache = self.combine_cache
return package
def get_publish_files(self):
u''' 发布目录所有的css/js文件 '''
def get_files(dir_path):
paths = []
for root, dirs, files in os.walk(dir_path):
if '.svn' in root:
dirs[:] = []
continue
for file in files:
path = os.path.join(root, file)
if os.path.splitext(path)[1] in ('.css', '.js'):
paths.append(path)
return paths
files = get_files(self.publish_path)
return files
def get_reverse_libs(self, all = False):
u''' 所有被依赖库 '''
libs = []
if self.workspace:
for local_path in self.workspace.url_packages.values():
package = self.get_package(local_path)
# 自己的路径在package相关library中,加入package的路径
if self.root in package.get_libs():
if package.root not in libs:
libs.append(package.root)
if all:
sublibs = package.get_reverse_libs(all = True)
libs.extend([subpath for subpath in sublibs if subpath not in libs and subpath != self.root])
return libs
def get_sub_packages(self):
subs = []
if self.workspace:
for package_root in self.workspace.local_packages:
if package_root != self.root and package_root.startswith(self.root):
subs.append(package_root)
else:
# 遍历磁盘
pass
return subs
def get_libs(self, all = False):
u''' 所有依赖库 '''
libs = []
def get_sub(local_path):
u'获取一个库的所有依赖库并存入libs'
package = self.get_package(local_path)
if package:
sublibs = package.get_libs(all = True)
libs.extend([subpath for subpath in sublibs if subpath not in libs])
if self.workspace:
if all:
# 获取sub_packages的所有依赖库
sub_packages = self.get_sub_packages()
for local_path in sub_packages:
get_sub(local_path)
for url in self.library_folders.values():
# url有可能写错了,或者url更改了
local_path = self.workspace.url_packages.get(url)
if local_path:
if local_path not in libs:
libs.append(local_path)
if all:
get_sub(local_path)
else:
raise PackageNotFoundException(url, self.root)
return libs
def get_relation_files(self, source, all = False):
u''' 在合并过程中相关的文件列表 '''
filetype = os.path.splitext(source)[1]
if filetype == '.css' and os.path.exists(source):
def pathTransformer(path):
return self.get_library_path(path)
imports, urls = csscompiler.getUrls(source, pathTransformer = pathTransformer, recursion = all, inAll = True)
urls.extend(imports)
# 需要监视的文件列表
files = [source]
for aurl in urls:
# 不支持 http:// 和 绝对路径
if not urlparse(aurl)[0] and not urlparse(aurl)[2].startswith('/'):
file = os.path.join(os.path.split(source)[0], aurl)
file = self.get_library_path(file)
files.append(file)
return files
elif filetype == '.js' and (source in self.combines.keys() or os.path.exists(source)):
if all:
return [file.path for file in self.get_combine_files(source)]
else:
return self.combines[source]
else:
# 永远返回一个数组
return []
def get_combine_included(self, file):
u''' 某个文件在当前库中被哪些文件引用了 '''
files = []
for combine in self.combines:
for include in self.combines[combine]:
if self.get_library_path(include) == file:
files.append(combine)
continue
return files
def get_included(self, source, all = False):
if os.path.splitext(source)[1] == '.css':
return []
else:
# 到workspace中找一圈
feed_files = []
if self.workspace:
for local_path in self.workspace.local_packages.keys():
package = self.get_package(local_path)
feed_files.extend(package.get_combine_included(source))
else:
feed_files = self.get_combine_included(source)
if all:
others = []
for feed_file in feed_files:
others.extend(self.get_included(feed_file, all = True))
feed_files.extend(others)
return feed_files
def get_combine_files(self, path):
u'''
@param path 执行合并的文件
@return combine_files 数组用来存储此文件相关的最小颗粒度的实体文件(非合并出来的文件)
'''
combine_files = [] # 存储整个合并过程中最小粒度的文件,最终会按照顺序进行合并
if path not in self.combines.keys():
return [File(path)]
for include in self.combines[path]:
include = self.get_library_path(include)
# package变量用来存储需要执行combine方法的package
# 本框架外部文件
if not include.startswith(self.source_path):
root_path = StaticPackage.get_root(include)
package = self.get_package(root_path)
package.parse(include)
# 默认为本package内部文件,为self
else:
package = self
# 一个合并出来的文件
if include in package.combines.keys():
rFiles = package.get_combine_files(include)
combine_files.extend(rFiles)
elif include.find('node_modules') != -1:
combine_files.append(ModuleFile(include, self.module_base))
# 一个最小粒度的文件
else:
combine_files.append(File(include))
return combine_files
def combine(self, output, files):
u''' 将files文件列表合并成一个文件并写入到output '''
text = ''
for file in files:
text += file.read()
target_dir = os.path.dirname(output)
if not os.path.exists(target_dir): os.makedirs(target_dir)
open(output, 'wb').write(text)
def compile(self, filename, force = False):
filename = os.path.realpath(filename)
source, mode = self.parse(filename)
if not source or not self.publish_path or not filename.startswith(self.publish_path):
return None, None
# 传进来的有可能是源文件路径 TODO
if filename.startswith(self.source_path):
return None, None
relation_files = self.get_relation_files(source, all = True)
if not relation_files:
# 没有源文件的发布文件
return None, None
modified, not_exists = self.listener.update(filename, relation_files)
filetype = os.path.splitext(filename)[1]
if filetype == '.js':
if force or len(modified):
combine_files = self.get_combine_files(source)
self.combine(filename, combine_files)
return modified, not_exists
elif filetype == '.css':
if DEBUG:
reload(csscompiler)
csscompiler.DEBUG = DEBUG
def pathTransformer(path):
return self.get_library_path(path)
if modified or force:
name = os.path.split(filename)[1]
cssId = hashlib.md5(urljoin('/' + urljoin(self.serverRoot, self.serverUrl), name)).hexdigest()[:8]
compiler = CSSCompiler(pathTransformer = pathTransformer)
css = compiler.compile(source, mode = mode, cssId = cssId)
css = self.replace_css_url(css, source, name)
self.write_file(filename, css)
return (modified, not_exists)
return None, None
def replace_css_url(self, css, source, target):
u''' 将css源文件中的url路径进行转换 '''
def replaceURL(m):
url = m.group(1)
urltuple = urlparse(url)
# 如果没有协议,则需要进行处理
if not urltuple[0]:
# 如果是绝对路径,则同serverPrefix+serverRoot进行拼接
if url.startswith('/'):
prefix = urljoin(self.serverPrefix, self.serverRoot)
url = urljoin(prefix, url)
# 如果是相对路径
else:
# 拼出prefix http://xnimg.cn/n/apps/msg/
prefix = urljoin(self.serverPrefix, self.serverRoot)
prefix = urljoin(prefix, self.serverUrl)
# 本地路径
path = os.path.realpath(os.path.join(os.path.split(source)[0], url))
# 拼出url css/global-all-min.css
relative = target[len(self.publish_path) + 1:].replace('\\', '/')
url = urljoin(relative, url)
# 在同一目录树的库中,判断url引用的是否是source目录中的东西
# 如果是source中的,source中的所有东西都会复制到publish中,因此不需要针对publish进行路径转换
# 如果是resource中的,resource会复制到publish中,需要进行路径转换
# 如果不是url引用的文件不是source中的,没有复制过程,需要针对publish进行路径转换
if self.resource_path and path.startswith(self.resource_path):
resource_publish_path = os.path.realpath(os.path.join(self.publish_path, self.resource_dir))
url = urljoin(relativeURI(path2uri(self.publish_path), path2uri(resource_publish_path)), url)
elif not path.startswith(self.source_path):
url = urljoin(relativeURI(path2uri(self.publish_path), path2uri(self.source_path)), url)
# 合并成最终的url http://xnimg.cn/n/core/css/global-all-min.css
url = urljoin(prefix, url)
return 'url(' + url + ')'
return re.sub(r'url\([\'"]?(.+?)[\'"]?\)', replaceURL, css) # 绝对路径
def build_files(self):
u''' 复制相关文件 '''
if not self.publish_path:
return []
files = self.build_source_files()
if self.resource_path:
files.extend(self.build_resource_files())
return files
def build_source_files(self):
# package下的所有资源文件,忽略 lib 目录
allFiles = []
for root, dirs, files in os.walk(self.source_path):
folders = re.split(r'[/\\]', root)
if '.svn' in folders \
or root.startswith(os.path.realpath(os.path.join(self.source_path, 'lib'))):
continue
for file in files:
if os.path.splitext(file)[1] in ('.css', '.xhtml', '.html', '.js', '.xsl', '.xml', '.un~', '.tmp', '.swp'): continue
path = os.path.join(root, file)
allFiles.append(path)
modified, notExists = self.listener.update(self.source_path, allFiles)
files = []
for file in modified:
target = os.path.join(self.publish_path, file[len(self.source_path) + 1:])
target_dir = os.path.dirname(target)
if not os.path.exists(target_dir):
os.makedirs(target_dir)
shutil.copy(file, target)
files.append(target)
return files
def build_resource_files(self):
# package下的所有资源文件,忽略 lib 目录
allFiles = []
for root, dirs, files in os.walk(self.resource_path):
folders = re.split(r'[/\\]', root)
if '.svn' in folders:
continue
for file in files:
path = os.path.join(root, file)
allFiles.append(path)
modified, notExists = self.listener.update(self.resource_path, allFiles)
files = []
for file in modified:
resource_publish_path = os.path.realpath(os.path.join(self.publish_path, self.resource_dir))
target = os.path.join(resource_publish_path, file[len(self.resource_path) + 1:])
target_dir = os.path.dirname(target)
if not os.path.exists(target_dir):
os.makedirs(target_dir)
shutil.copy(file, target)
files.append(target)
return files
def write_file(self, path, txt):
cssfile = open(path, 'wb')
cssfile.write(txt)
cssfile.close()
def joinpath(self, path1, path2):
return os.path.realpath(os.path.join(path1, path2))
def parse_config(self, xmlConfig):
# 已通过source文件读取到publish_path信息
# 或者已经通过构造函数传进了publish_path信息
# 不用通过template-config.xml读取
# source引用方式下,不需要配置文件的publish配置
if self.publish_path:
pass
# 没有source文件,publish_path同库在同一个目录树,需要通过publish配置生成publish_path信息
else:
publishNode = xmlConfig.find('publish')
if publishNode != None:
publishDir = xmlConfig.find('publish').get('dir')
if not publishDir.endswith('/'): publishDir += '/'
self.publish_path = self.joinpath(self.root, publishDir)
self.url = xmlConfig.get('url')
# source 是必需的
sourceDir = xmlConfig.find('source').attrib['dir']
if not sourceDir.endswith('/'): sourceDir += '/'
self.source_path = self.joinpath(self.root, sourceDir)
libraryNode = xmlConfig.find('library')
if libraryNode != None:
libraryDir = libraryNode.get('dir')
self.library_path = self.joinpath(self.root, libraryDir)
folderNodes = libraryNode.findall('folder')
for folderNode in folderNodes:
self.library_folders[folderNode.get('name')] = folderNode.get('url')
resourceNode = xmlConfig.find('resource')
if resourceNode != None and 'dir' in resourceNode.attrib.keys():
self.resource_dir = resourceNode.attrib['dir']
if not self.resource_dir.endswith('/'): self.resource_dir += '/'
self.resource_path = self.joinpath(self.root, self.resource_dir)
serverNode = xmlConfig.find('server')
if serverNode != None:
if 'prefix' in serverNode.attrib:
self.serverPrefix = serverNode.attrib['prefix']
if 'url' in serverNode.attrib:
self.serverUrl = serverNode.attrib['url']
if 'root' in serverNode.attrib:
self.serverRoot = serverNode.attrib['root']
if self.serverUrl and not self.serverUrl.endswith('/'):
self.serverUrl = self.serverUrl + '/'
if self.serverRoot and not self.serverRoot.endswith('/'):
self.serverRoot = self.serverRoot + '/'
self.module_base = xmlConfig.get('module-base')
combinesXML = xmlConfig.findall('source/combine')
if combinesXML:
for combine in combinesXML:
key = self.joinpath(self.source_path, combine.get('path'))
includesXML = combine.findall('include')
includes = []
for include in includesXML:
if include.get('module'):
includePath = self.joinpath(self.source_path, 'node_modules/' + include.get('module'))
else:
includePath = self.joinpath(self.source_path, include.get('path'))
includes.append(includePath)
self.combines[key] = includes
def load_config(self):
''' 解析配置文件 '''
path = os.path.join(self.root, CONFIG_FILENAME)
xmlConfig = ElementTree.parse(path)
self.parse_config(xmlConfig.getroot())
def get_library_path(self, includePath):
includePath = os.path.realpath(includePath)
# lib下的,要通过packages转换一下路径
if includePath.startswith(self.library_path):
path = includePath[len(self.library_path) + 1:]
if path.find(os.sep) != -1: # lib/xxx.js 直接放,没有在一个library目录中
folderName, pathInPackage = path.split(os.sep, 1) # lib下的目录名和相应package的内部路径
if folderName in self.library_folders.keys():
url = self.library_folders[folderName]
if not self.workspace:
raise WorkspaceNotFoundException()
local_path = self.workspace.url_packages.get(url)
if local_path:
package = self.get_package(local_path)
new_path = os.path.join(package.root, pathInPackage)
return os.path.realpath(new_path)
# 在lib下,也定义了folder,但是相对应的url没有在packages中配置本地磁盘的路径,或者相应的库没有配置package的url属性
else:
raise PackageNotFoundException(url, self.root)
return includePath
def parse(self, path):
u''' source 和 publish 路径一一对应 '''
path = os.path.realpath(path)
if path.startswith(self.source_path):
return path, None
elif self.publish_path and path.startswith(self.publish_path):
package_path = path[len(self.publish_path) + 1:]
if os.path.splitext(path)[1] == '.css':
# xxx-all-min.css --> xxx
package_path = os.path.splitext(package_path)[0].split('-')
if len(package_path) < 3: return (None, None)
name = '-'.join(package_path[:-2])
mode = package_path[-2]
source = os.path.join(self.source_path, name + '.css')
else:
source = os.path.join(self.source_path, package_path)
mode = None
return source, mode
# 有可能是lib下的
else:
return None, None
def link(self):
u''' 连接源库与发布库 '''
if self.url:
package_file_path = os.path.join(self.publish_path, PACKAGE_FILENAME)
open(package_file_path, 'wb').write(self.url)
source_path = os.path.join(self.publish_path, SOURCE_FILENAME)
source_dir = os.path.dirname(source_path)
if not os.path.exists(source_dir):
os.makedirs(source_dir)
open(source_path, 'wb').write(self.root)
@staticmethod
def init(root_path):
u''' 初始化一个目录为源库 '''
root_path = os.path.realpath(root_path)
config_path = os.path.join(root_path, CONFIG_FILENAME)
if os.path.exists(config_path):
raise PackageExistsException(root_path)
if not os.path.exists(root_path):
os.makedirs(root_path)
open(config_path, 'wb').write(
'<package>\n\t<library dir="lib">\n\t</library>\n\t<source dir="src">\n\t</source>\n\t<resource dir="res">\n\t</resource>\n</package>'
)
@staticmethod
def is_root(path):
u''' path是否是一个静态库的根路径 '''
return os.path.exists(os.path.join(path, CONFIG_FILENAME))
@staticmethod
def get_root(path):
u'''一个目录的源库根路径'''
return StaticPackage.get_roots(path)[1]
@staticmethod
def get_publish(path):
u''' 一个路径的发布库根路径 '''
return StaticPackage.get_roots(path, just_publish_path = True)
@staticmethod
def get_roots(path, just_publish_path = False, workspace = None):
u''' 通过遍历父目录查找root及publish_path '''
publish_path = None
root_path = None
if os.path.isfile(path):
path = os.path.dirname(path)
while True:
# 通过找 source 的方式读取到地址
publish_source_path = os.path.join(path, SOURCE_FILENAME)
if os.path.exists(publish_source_path):
publish_path = path
root_path = os.path.realpath(open(publish_source_path, 'r').read().strip())
if just_publish_path: break
# 通过.package 文件读取到地址
if workspace:
package_file_path = os.path.join(path, PACKAGE_FILENAME)
if os.path.exists(package_file_path):
publish_path = path
package_url = open(package_file_path, 'r').read().strip()
package = workspace.get_package_by_url(package_url)
if not package:
raise PackageNotFoundException(package_url)
root_path = package.root
if just_publish_path: break
# 直接找到配置文件
if not just_publish_path and StaticPackage.is_root(path):
root_path = os.path.realpath(path)
break
newpath = os.path.realpath(os.path.join(path, '../'))
if newpath == path: break # 已经到根目录了,停止循环
else: path = newpath
if just_publish_path:
return publish_path
else:
return publish_path, root_path
class Workspace():
u''' 工作区 '''
def __init__(self, root):
self.root = root
self.packages_file_path = os.path.join(self.root, PACKAGES_FILENAME)
self.url_packages = {}
self.local_packages = {}
self.useless_packages = []
self.remote_server = 'http://hg.xnimg.cn/'
self.init_packages()
def init_packages(self):
if not os.path.exists(self.packages_file_path): return
packages_file = open(self.packages_file_path, 'r').read().strip()
if packages_file:
lines = packages_file.split('\n')
else:
lines = []
for package_path in lines:
package_path, publish_path = re.match('^(.+?)\s*(?:=\s*(.+)?)?$', package_path.strip()).groups()
local_path = os.path.realpath(os.path.join(self.root, package_path))
self.add_package(local_path)
def remote2local(self, package):
u''' 将一个remotepackage的地址转换成当在本地时的地址 '''
return os.path.realpath(os.path.join(self.root, package.hg_dir))
def fetch_packages(self, package_url):
u''' 根据package url找到所有相关package '''
remote_workspace = RemoteWorkspace(self.remote_server)
if package_url not in remote_workspace.url_packages.keys():
raise PackageNotFoundException(package_url, self.root)
path = remote_workspace.url_packages[package_url]
package = RemoteStaticPackage(path, workspace = remote_workspace)
packages = [path]
for local_path in package.get_libs(all = True):
packages.append(local_path)
# 所有子库的相关依赖库
for sub in package.subs:
sub = package.get_package(sub)
for local_path in sub.get_libs(all = True):
if local_path not in packages:
packages.append(local_path)
packages = [package.get_package(root_path) for root_path in packages]
return packages
def fetch(self, package):
u''' 将一个package下载到本地工作区 '''
# hg update
def hg_update(local_path):
dir = os.path.realpath(os.curdir)
os.chdir(local_path)
a = os.system('hg update')
os.chdir(dir)
# hg clone
def hg_clone(url, local_path, noupdate = False):
#print 'hg clone ' + ('--noupdate ' if noupdate else '') + '%s %s' % (url, local_path)
os.system('hg clone ' + ('--noupdate ' if noupdate else '') + '%s %s' % (url, local_path))
local_path = self.remote2local(package)
# 本地已经有这个package了
if os.path.exists(local_path):
hg_update(local_path)
# 本地存放的路径有可能与远程并不相同
elif package.url in self.url_packages:
local_path = self.url_packages[package.url]
hg_update(local_path)
# 本地没有,按照远程的路径获取
else:
self.add_package(local_path)
# 处理父路径
for parent in package.parents:
parent = package.get_package(parent)
parent_local_path = os.path.realpath(os.path.join(self.root, parent.hg_dir))
if not os.path.exists(parent_local_path):
hg_clone(parent.hg_root, parent_local_path, noupdate = True)
hg_clone(package.hg_root, local_path)
# 处理子库,加入workspace
for sub in package.subs:
sub = package.get_package(sub)
self.add_package(self.remote2local(sub))
def load(self):
for root, dirs, files in os.walk(self.root):
if StaticPackage.is_root(root):
if not self.has_package(root):
self.add_package(root)
self.rebuild_package()
def get_package_by_url(self, url):
local_path = self.url_packages.get(url)
if local_path:
package = StaticPackage(local_path, workspace = self)
return package
def has_package(self, root_path):
root_path = os.path.realpath(root_path)
return root_path in self.local_packages.keys()
def add_package(self, local_path):
local_path = os.path.realpath(local_path)
if not local_path.startswith(os.path.realpath(self.root)):
raise NotInWorkspaceException
config_path = os.path.join(local_path, CONFIG_FILENAME)
if not os.path.exists(config_path):
self.useless_packages.append(local_path)
else:
try:
package_config = ElementTree.parse(config_path)
except BaseException as e:
raise ConfigError(config_path)
else:
package_url = package_config.getroot().get('url')
self.local_packages[local_path] = package_url
if package_url: self.url_packages[package_url] = local_path
self.rebuild_package()
def rebuild_package(self):
# 不要在fastcgi中rebuild_package,因为没有文件锁,会出问题,给出提示即可。
packages_file = open(self.packages_file_path, 'wb')
packages = self.local_packages.keys()
packages.sort()
for local_path in packages:
packages_file.write(self.transform_name(local_path) + '\n')
packages_file.close()
def transform_name(self, path):
return path2uri(path[len(self.root) + 1:])
@staticmethod
def get_workspace(workspace_path):
u''' 源库所在工作区 '''
while True:
if Workspace.is_root(workspace_path):
return workspace_path
else:
newpath = os.path.realpath(os.path.join(workspace_path, '../'))
if newpath == workspace_path: return
else: workspace_path = newpath
return None
@staticmethod
def is_root(path):
u''' path是否是一个工作区的根路径 '''
return os.path.exists(os.path.join(path, PACKAGES_FILENAME))
class RemoteWorkspace(Workspace):
u'远程Workspace'
def init_packages(self):
sock = urllib2.urlopen(self.root + '_/raw-file/tip/.packages')
lines = sock.read().strip().split('\n')
for package_path in lines:
package_path, publish_path = re.match('^(.+?)\s*(?:=\s*(.+)?)?$', package_path.strip()).groups()
remote_path = urljoin(self.root, package_path + '/raw-file/tip/')
config_path = urljoin(remote_path, CONFIG_FILENAME)
try:
sock = urllib2.urlopen(config_path)
except:
self.useless_packages.append(remote_path)
else:
try:
package_config = ElementTree.fromstring(sock.read())
except BaseException as e:
raise ConfigError(config_path)
else:
package_url = package_config.get('url')
self.local_packages[remote_path] = package_url
if package_url: self.url_packages[package_url] = remote_path
sock.close()
sock.close()
class RemoteStaticPackage(StaticPackage):
u'远程静态库'
def __init__(self, root_path, publish_path = None, workspace = None, listener = None):
StaticPackage.__init__(self, root_path, publish_path = publish_path, workspace = workspace, listener = listener)
self.hg_root = self.get_hg_path(root_path)
self.hg_dir = self.get_hg_path(root_path[len(self.workspace.root):])
parents = []
subs = []
hg_root = self.get_hg_path(self.root)
for path in self.workspace.local_packages:
if path != self.root:
if self.root.startswith(self.get_hg_path(path)):
parents.append(path)
elif path.startswith(hg_root):
subs.append(path)
# 按照路径长短排序,短的排前面,确保生成路径时先生成短的,先生成长的会导致短的无法生成
self.parents = sorted(parents, cmp = lambda x, y: cmp(len(x), len(y)))
self.subs = sorted(subs, cmp = lambda x, y: cmp(len(x), len(y)))
def joinpath(self, path1, path2):
return urljoin(path1, path2)
def load_config(self):
''' 解析配置文件 '''
config_path = urljoin(self.root, CONFIG_FILENAME)
sock = urllib2.urlopen(config_path)
package_config = ElementTree.fromstring(sock.read())
self.parse_config(package_config)
sock.close()
def get_package(self, root_path):
u''' 从缓存中获取package引用,如果没有则生成新的并加入缓存 '''
package = self.combine_cache.get(root_path)
if not package:
package = RemoteStaticPackage(root_path, self.publish_path, workspace = self.workspace, listener = self.listener)
self.combine_cache[root_path] = package
package.combine_cache = self.combine_cache
return package
@staticmethod
def get_hg_path(path):
if path.endswith('/raw-file/tip/'):
return path[:-13] # remove /raw-file/tip/ at the end
else:
return path