1# Status: this module is ported on demand by however needs something 2# from it. Functionality that is not needed by Python port will 3# be dropped. 4 5# Copyright (C) Vladimir Prus 2002. Permission to copy, use, modify, sell and 6# distribute this software is granted provided this copyright notice appears in 7# all copies. This software is provided "as is" without express or implied 8# warranty, and with no claim as to its suitability for any purpose. 9 10# Performs various path manipulations. Path are always in a 'normilized' 11# representation. In it, a path may be either: 12# 13# - '.', or 14# 15# - ['/'] [ ( '..' '/' )* (token '/')* token ] 16# 17# In plain english, path can be rooted, '..' elements are allowed only 18# at the beginning, and it never ends in slash, except for path consisting 19# of slash only. 20 21import os.path 22from utility import to_seq 23from glob import glob as builtin_glob 24 25from b2.util import bjam_signature 26 27@bjam_signature((["path", "root"],)) 28def root (path, root): 29 """ If 'path' is relative, it is rooted at 'root'. Otherwise, it's unchanged. 30 """ 31 if os.path.isabs (path): 32 return path 33 else: 34 return os.path.join (root, path) 35 36@bjam_signature((["native"],)) 37def make (native): 38 """ Converts the native path into normalized form. 39 """ 40 # TODO: make os selection here. 41 return make_UNIX (native) 42 43@bjam_signature([['native']]) 44def make_UNIX (native): 45 46 # VP: I have no idea now 'native' can be empty here! But it can! 47 assert (native) 48 49 return os.path.normpath (native) 50 51@bjam_signature((["path"],)) 52def native (path): 53 """ Builds a native representation of the path. 54 """ 55 # TODO: make os selection here. 56 return native_UNIX (path) 57 58def native_UNIX (path): 59 return path 60 61 62def pwd (): 63 """ Returns the current working directory. 64 # TODO: is it a good idea to use the current dir? Some use-cases 65 may not allow us to depend on the current dir. 66 """ 67 return make (os.getcwd ()) 68 69def is_rooted (path): 70 """ Tests if a path is rooted. 71 """ 72 return path and path [0] == '/' 73 74 75################################################################### 76# Still to port. 77# Original lines are prefixed with "# " 78# 79# # Copyright (C) Vladimir Prus 2002. Permission to copy, use, modify, sell and 80# # distribute this software is granted provided this copyright notice appears in 81# # all copies. This software is provided "as is" without express or implied 82# # warranty, and with no claim as to its suitability for any purpose. 83# 84# # Performs various path manipulations. Path are always in a 'normilized' 85# # representation. In it, a path may be either: 86# # 87# # - '.', or 88# # 89# # - ['/'] [ ( '..' '/' )* (token '/')* token ] 90# # 91# # In plain english, path can be rooted, '..' elements are allowed only 92# # at the beginning, and it never ends in slash, except for path consisting 93# # of slash only. 94# 95# import modules ; 96# import sequence ; 97# import regex ; 98# import errors : error ; 99# 100# 101# os = [ modules.peek : OS ] ; 102# if [ modules.peek : UNIX ] 103# { 104# local uname = [ modules.peek : JAMUNAME ] ; 105# switch $(uname) 106# { 107# case CYGWIN* : 108# os = CYGWIN ; 109# 110# case * : 111# os = UNIX ; 112# } 113# } 114# 115# # 116# # Tests if a path is rooted. 117# # 118# rule is-rooted ( path ) 119# { 120# return [ MATCH "^(/)" : $(path) ] ; 121# } 122# 123# # 124# # Tests if a path has a parent. 125# # 126# rule has-parent ( path ) 127# { 128# if $(path) != / { 129# return 1 ; 130# } else { 131# return ; 132# } 133# } 134# 135# # 136# # Returns the path without any directory components. 137# # 138# rule basename ( path ) 139# { 140# return [ MATCH "([^/]+)$" : $(path) ] ; 141# } 142# 143# # 144# # Returns parent directory of the path. If no parent exists, error is issued. 145# # 146# rule parent ( path ) 147# { 148# if [ has-parent $(path) ] { 149# 150# if $(path) = . { 151# return .. ; 152# } else { 153# 154# # Strip everything at the end of path up to and including 155# # the last slash 156# local result = [ regex.match "((.*)/)?([^/]+)" : $(path) : 2 3 ] ; 157# 158# # Did we strip what we shouldn't? 159# if $(result[2]) = ".." { 160# return $(path)/.. ; 161# } else { 162# if ! $(result[1]) { 163# if [ is-rooted $(path) ] { 164# result = / ; 165# } else { 166# result = . ; 167# } 168# } 169# return $(result[1]) ; 170# } 171# } 172# } else { 173# error "Path '$(path)' has no parent" ; 174# } 175# } 176# 177# # 178# # Returns path2 such that "[ join path path2 ] = .". 179# # The path may not contain ".." element or be rooted. 180# # 181# rule reverse ( path ) 182# { 183# if $(path) = . 184# { 185# return $(path) ; 186# } 187# else 188# { 189# local tokens = [ regex.split $(path) "/" ] ; 190# local tokens2 ; 191# for local i in $(tokens) { 192# tokens2 += .. ; 193# } 194# return [ sequence.join $(tokens2) : "/" ] ; 195# } 196# } 197def reverse(path): 198 """Returns path2 such that `os.path.join(path, path2) == '.'`. 199 `path` may not contain '..' or be rooted. 200 201 Args: 202 path (str): the path to reverse 203 204 Returns: 205 the string of the reversed path 206 207 Example: 208 209 >>> p1 = 'path/to/somewhere' 210 >>> p2 = reverse('path/to/somewhere') 211 >>> p2 212 '../../..' 213 >>> os.path.normpath(os.path.join(p1, p2)) 214 '.' 215 """ 216 if is_rooted(path) or '..' in path: 217 from b2.manager import get_manager 218 get_manager().errors()( 219 'reverse(path): path is either rooted or contains ".." in the path') 220 if path == '.': 221 return path 222 path = os.path.normpath(path) 223 # os.sep.join() is being used over os.path.join() due 224 # to an extra '..' that is created by os.path.join() 225 return os.sep.join('..' for t in path.split(os.sep)) 226# # 227# # Auxiliary rule: does all the semantic of 'join', except for error checking. 228# # The error checking is separated because this rule is recursive, and I don't 229# # like the idea of checking the same input over and over. 230# # 231# local rule join-imp ( elements + ) 232# { 233# return [ NORMALIZE_PATH $(elements:J="/") ] ; 234# } 235# 236# # 237# # Contanenates the passed path elements. Generates an error if 238# # any element other than the first one is rooted. 239# # 240# rule join ( elements + ) 241# { 242# if ! $(elements[2]) 243# { 244# return $(elements[1]) ; 245# } 246# else 247# { 248# for local e in $(elements[2-]) 249# { 250# if [ is-rooted $(e) ] 251# { 252# error only first element may be rooted ; 253# } 254# } 255# return [ join-imp $(elements) ] ; 256# } 257# } 258 259 260def glob (dirs, patterns): 261 """ Returns the list of files matching the given pattern in the 262 specified directory. Both directories and patterns are 263 supplied as portable paths. Each pattern should be non-absolute 264 path, and can't contain "." or ".." elements. Each slash separated 265 element of pattern can contain the following special characters: 266 - '?', which match any character 267 - '*', which matches arbitrary number of characters. 268 A file $(d)/e1/e2/e3 (where 'd' is in $(dirs)) matches pattern p1/p2/p3 269 if and only if e1 matches p1, e2 matches p2 and so on. 270 271 For example: 272 [ glob . : *.cpp ] 273 [ glob . : */build/Jamfile ] 274 """ 275# { 276# local result ; 277# if $(patterns:D) 278# { 279# # When a pattern has a directory element, we first glob for 280# # directory, and then glob for file name is the found directories. 281# for local p in $(patterns) 282# { 283# # First glob for directory part. 284# local globbed-dirs = [ glob $(dirs) : $(p:D) ] ; 285# result += [ glob $(globbed-dirs) : $(p:D="") ] ; 286# } 287# } 288# else 289# { 290# # When a pattern has not directory, we glob directly. 291# # Take care of special ".." value. The "GLOB" rule simply ignores 292# # the ".." element (and ".") element in directory listings. This is 293# # needed so that 294# # 295# # [ glob libs/*/Jamfile ] 296# # 297# # don't return 298# # 299# # libs/../Jamfile (which is the same as ./Jamfile) 300# # 301# # On the other hand, when ".." is explicitly present in the pattern 302# # we need to return it. 303# # 304# for local dir in $(dirs) 305# { 306# for local p in $(patterns) 307# { 308# if $(p) != ".." 309# { 310# result += [ sequence.transform make 311# : [ GLOB [ native $(dir) ] : $(p) ] ] ; 312# } 313# else 314# { 315# result += [ path.join $(dir) .. ] ; 316# } 317# } 318# } 319# } 320# return $(result) ; 321# } 322# 323 324# TODO: (PF) I replaced the code above by this. I think it should work but needs to be tested. 325 result = [] 326 dirs = to_seq (dirs) 327 patterns = to_seq (patterns) 328 329 splitdirs = [] 330 for dir in dirs: 331 splitdirs += dir.split (os.pathsep) 332 333 for dir in splitdirs: 334 for pattern in patterns: 335 p = os.path.join (dir, pattern) 336 import glob 337 result.extend (glob.glob (p)) 338 return result 339 340# 341# Find out the absolute name of path and returns the list of all the parents, 342# starting with the immediate one. Parents are returned as relative names. 343# If 'upper_limit' is specified, directories above it will be pruned. 344# 345def all_parents(path, upper_limit=None, cwd=None): 346 347 if not cwd: 348 cwd = os.getcwd() 349 350 path_abs = os.path.join(cwd, path) 351 352 if upper_limit: 353 upper_limit = os.path.join(cwd, upper_limit) 354 355 result = [] 356 while path_abs and path_abs != upper_limit: 357 (head, tail) = os.path.split(path) 358 path = os.path.join(path, "..") 359 result.append(path) 360 path_abs = head 361 362 if upper_limit and path_abs != upper_limit: 363 raise BaseException("'%s' is not a prefix of '%s'" % (upper_limit, path)) 364 365 return result 366 367# Search for 'pattern' in parent directories of 'dir', up till and including 368# 'upper_limit', if it is specified, or till the filesystem root otherwise. 369# 370def glob_in_parents(dir, patterns, upper_limit=None): 371 372 result = [] 373 parent_dirs = all_parents(dir, upper_limit) 374 375 for p in parent_dirs: 376 result = glob(p, patterns) 377 if result: break 378 379 return result 380 381# 382# # 383# # Assuming 'child' is a subdirectory of 'parent', return the relative 384# # path from 'parent' to 'child' 385# # 386# rule relative ( child parent ) 387# { 388# if $(parent) = "." 389# { 390# return $(child) ; 391# } 392# else 393# { 394# local split1 = [ regex.split $(parent) / ] ; 395# local split2 = [ regex.split $(child) / ] ; 396# 397# while $(split1) 398# { 399# if $(split1[1]) = $(split2[1]) 400# { 401# split1 = $(split1[2-]) ; 402# split2 = $(split2[2-]) ; 403# } 404# else 405# { 406# errors.error $(child) is not a subdir of $(parent) ; 407# } 408# } 409# return [ join $(split2) ] ; 410# } 411# } 412# 413# # Returns the minimal path to path2 that is relative path1. 414# # 415# rule relative-to ( path1 path2 ) 416# { 417# local root_1 = [ regex.split [ reverse $(path1) ] / ] ; 418# local split1 = [ regex.split $(path1) / ] ; 419# local split2 = [ regex.split $(path2) / ] ; 420# 421# while $(split1) && $(root_1) 422# { 423# if $(split1[1]) = $(split2[1]) 424# { 425# root_1 = $(root_1[2-]) ; 426# split1 = $(split1[2-]) ; 427# split2 = $(split2[2-]) ; 428# } 429# else 430# { 431# split1 = ; 432# } 433# } 434# return [ join . $(root_1) $(split2) ] ; 435# } 436 437# Returns the list of paths which are used by the operating system 438# for looking up programs 439def programs_path (): 440 raw = [] 441 names = ['PATH', 'Path', 'path'] 442 443 for name in names: 444 raw.append(os.environ.get (name, '')) 445 446 result = [] 447 for elem in raw: 448 if elem: 449 for p in elem.split(os.path.pathsep): 450 # it's possible that the user's Path has 451 # double path separators, thus it is possible 452 # for p to be an empty string. 453 if p: 454 result.append(make(p)) 455 456 return result 457 458# rule make-NT ( native ) 459# { 460# local tokens = [ regex.split $(native) "[/\\]" ] ; 461# local result ; 462# 463# # Handle paths ending with slashes 464# if $(tokens[-1]) = "" 465# { 466# tokens = $(tokens[1--2]) ; # discard the empty element 467# } 468# 469# result = [ path.join $(tokens) ] ; 470# 471# if [ regex.match "(^.:)" : $(native) ] 472# { 473# result = /$(result) ; 474# } 475# 476# if $(native) = "" 477# { 478# result = "." ; 479# } 480# 481# return $(result) ; 482# } 483# 484# rule native-NT ( path ) 485# { 486# local result = [ MATCH "^/?(.*)" : $(path) ] ; 487# result = [ sequence.join [ regex.split $(result) "/" ] : "\\" ] ; 488# return $(result) ; 489# } 490# 491# rule make-CYGWIN ( path ) 492# { 493# return [ make-NT $(path) ] ; 494# } 495# 496# rule native-CYGWIN ( path ) 497# { 498# local result = $(path) ; 499# if [ regex.match "(^/.:)" : $(path) ] # win absolute 500# { 501# result = [ MATCH "^/?(.*)" : $(path) ] ; # remove leading '/' 502# } 503# return [ native-UNIX $(result) ] ; 504# } 505# 506# # 507# # split-VMS: splits input native path into 508# # device dir file (each part is optional), 509# # example: 510# # 511# # dev:[dir]file.c => dev: [dir] file.c 512# # 513# rule split-path-VMS ( native ) 514# { 515# local matches = [ MATCH ([a-zA-Z0-9_-]+:)?(\\[[^\]]*\\])?(.*)?$ : $(native) ] ; 516# local device = $(matches[1]) ; 517# local dir = $(matches[2]) ; 518# local file = $(matches[3]) ; 519# 520# return $(device) $(dir) $(file) ; 521# } 522# 523# # 524# # Converts a native VMS path into a portable path spec. 525# # 526# # Does not handle current-device absolute paths such 527# # as "[dir]File.c" as it is not clear how to represent 528# # them in the portable path notation. 529# # 530# # Adds a trailing dot (".") to the file part if no extension 531# # is present (helps when converting it back into native path). 532# # 533# rule make-VMS ( native ) 534# { 535# if [ MATCH ^(\\[[a-zA-Z0-9]) : $(native) ] 536# { 537# errors.error "Can't handle default-device absolute paths: " $(native) ; 538# } 539# 540# local parts = [ split-path-VMS $(native) ] ; 541# local device = $(parts[1]) ; 542# local dir = $(parts[2]) ; 543# local file = $(parts[3]) ; 544# local elems ; 545# 546# if $(device) 547# { 548# # 549# # rooted 550# # 551# elems = /$(device) ; 552# } 553# 554# if $(dir) = "[]" 555# { 556# # 557# # Special case: current directory 558# # 559# elems = $(elems) "." ; 560# } 561# else if $(dir) 562# { 563# dir = [ regex.replace $(dir) "\\[|\\]" "" ] ; 564# local dir_parts = [ regex.split $(dir) \\. ] ; 565# 566# if $(dir_parts[1]) = "" 567# { 568# # 569# # Relative path 570# # 571# dir_parts = $(dir_parts[2--1]) ; 572# } 573# 574# # 575# # replace "parent-directory" parts (- => ..) 576# # 577# dir_parts = [ regex.replace-list $(dir_parts) : - : .. ] ; 578# 579# elems = $(elems) $(dir_parts) ; 580# } 581# 582# if $(file) 583# { 584# if ! [ MATCH (\\.) : $(file) ] 585# { 586# # 587# # Always add "." to end of non-extension file 588# # 589# file = $(file). ; 590# } 591# elems = $(elems) $(file) ; 592# } 593# 594# local portable = [ path.join $(elems) ] ; 595# 596# return $(portable) ; 597# } 598# 599# # 600# # Converts a portable path spec into a native VMS path. 601# # 602# # Relies on having at least one dot (".") included in the file 603# # name to be able to differentiate it ftom the directory part. 604# # 605# rule native-VMS ( path ) 606# { 607# local device = "" ; 608# local dir = $(path) ; 609# local file = "" ; 610# local native ; 611# local split ; 612# 613# # 614# # Has device ? 615# # 616# if [ is-rooted $(dir) ] 617# { 618# split = [ MATCH ^/([^:]+:)/?(.*) : $(dir) ] ; 619# device = $(split[1]) ; 620# dir = $(split[2]) ; 621# } 622# 623# # 624# # Has file ? 625# # 626# # This is no exact science, just guess work: 627# # 628# # If the last part of the current path spec 629# # includes some chars, followed by a dot, 630# # optionally followed by more chars - 631# # then it is a file (keep your fingers crossed). 632# # 633# split = [ regex.split $(dir) / ] ; 634# local maybe_file = $(split[-1]) ; 635# 636# if [ MATCH ^([^.]+\\..*) : $(maybe_file) ] 637# { 638# file = $(maybe_file) ; 639# dir = [ sequence.join $(split[1--2]) : / ] ; 640# } 641# 642# # 643# # Has dir spec ? 644# # 645# if $(dir) = "." 646# { 647# dir = "[]" ; 648# } 649# else if $(dir) 650# { 651# dir = [ regex.replace $(dir) \\.\\. - ] ; 652# dir = [ regex.replace $(dir) / . ] ; 653# 654# if $(device) = "" 655# { 656# # 657# # Relative directory 658# # 659# dir = "."$(dir) ; 660# } 661# dir = "["$(dir)"]" ; 662# } 663# 664# native = [ sequence.join $(device) $(dir) $(file) ] ; 665# 666# return $(native) ; 667# } 668# 669# 670# rule __test__ ( ) { 671# 672# import assert ; 673# import errors : try catch ; 674# 675# assert.true is-rooted "/" ; 676# assert.true is-rooted "/foo" ; 677# assert.true is-rooted "/foo/bar" ; 678# assert.result : is-rooted "." ; 679# assert.result : is-rooted "foo" ; 680# assert.result : is-rooted "foo/bar" ; 681# 682# assert.true has-parent "foo" ; 683# assert.true has-parent "foo/bar" ; 684# assert.true has-parent "." ; 685# assert.result : has-parent "/" ; 686# 687# assert.result "." : basename "." ; 688# assert.result ".." : basename ".." ; 689# assert.result "foo" : basename "foo" ; 690# assert.result "foo" : basename "bar/foo" ; 691# assert.result "foo" : basename "gaz/bar/foo" ; 692# assert.result "foo" : basename "/gaz/bar/foo" ; 693# 694# assert.result "." : parent "foo" ; 695# assert.result "/" : parent "/foo" ; 696# assert.result "foo/bar" : parent "foo/bar/giz" ; 697# assert.result ".." : parent "." ; 698# assert.result ".." : parent "../foo" ; 699# assert.result "../../foo" : parent "../../foo/bar" ; 700# 701# 702# assert.result "." : reverse "." ; 703# assert.result ".." : reverse "foo" ; 704# assert.result "../../.." : reverse "foo/bar/giz" ; 705# 706# assert.result "foo" : join "foo" ; 707# assert.result "/foo" : join "/" "foo" ; 708# assert.result "foo/bar" : join "foo" "bar" ; 709# assert.result "foo/bar" : join "foo/giz" "../bar" ; 710# assert.result "foo/giz" : join "foo/bar/baz" "../../giz" ; 711# assert.result ".." : join "." ".." ; 712# assert.result ".." : join "foo" "../.." ; 713# assert.result "../.." : join "../foo" "../.." ; 714# assert.result "/foo" : join "/bar" "../foo" ; 715# assert.result "foo/giz" : join "foo/giz" "." ; 716# assert.result "." : join lib2 ".." ; 717# assert.result "/" : join "/a" ".." ; 718# 719# assert.result /a/b : join /a/b/c .. ; 720# 721# assert.result "foo/bar/giz" : join "foo" "bar" "giz" ; 722# assert.result "giz" : join "foo" ".." "giz" ; 723# assert.result "foo/giz" : join "foo" "." "giz" ; 724# 725# try ; 726# { 727# join "a" "/b" ; 728# } 729# catch only first element may be rooted ; 730# 731# local CWD = "/home/ghost/build" ; 732# assert.result : all-parents . : . : $(CWD) ; 733# assert.result . .. ../.. ../../.. : all-parents "Jamfile" : "" : $(CWD) ; 734# assert.result foo . .. ../.. ../../.. : all-parents "foo/Jamfile" : "" : $(CWD) ; 735# assert.result ../Work .. ../.. ../../.. : all-parents "../Work/Jamfile" : "" : $(CWD) ; 736# 737# local CWD = "/home/ghost" ; 738# assert.result . .. : all-parents "Jamfile" : "/home" : $(CWD) ; 739# assert.result . : all-parents "Jamfile" : "/home/ghost" : $(CWD) ; 740# 741# assert.result "c/d" : relative "a/b/c/d" "a/b" ; 742# assert.result "foo" : relative "foo" "." ; 743# 744# local save-os = [ modules.peek path : os ] ; 745# modules.poke path : os : NT ; 746# 747# assert.result "foo/bar/giz" : make "foo/bar/giz" ; 748# assert.result "foo/bar/giz" : make "foo\\bar\\giz" ; 749# assert.result "foo" : make "foo/." ; 750# assert.result "foo" : make "foo/bar/.." ; 751# assert.result "/D:/My Documents" : make "D:\\My Documents" ; 752# assert.result "/c:/boost/tools/build/new/project.jam" : make "c:\\boost\\tools\\build\\test\\..\\new\\project.jam" ; 753# 754# assert.result "foo\\bar\\giz" : native "foo/bar/giz" ; 755# assert.result "foo" : native "foo" ; 756# assert.result "D:\\My Documents\\Work" : native "/D:/My Documents/Work" ; 757# 758# modules.poke path : os : UNIX ; 759# 760# assert.result "foo/bar/giz" : make "foo/bar/giz" ; 761# assert.result "/sub1" : make "/sub1/." ; 762# assert.result "/sub1" : make "/sub1/sub2/.." ; 763# assert.result "sub1" : make "sub1/." ; 764# assert.result "sub1" : make "sub1/sub2/.." ; 765# assert.result "/foo/bar" : native "/foo/bar" ; 766# 767# modules.poke path : os : VMS ; 768# 769# # 770# # Don't really need to poke os before these 771# # 772# assert.result "disk:" "[dir]" "file" : split-path-VMS "disk:[dir]file" ; 773# assert.result "disk:" "[dir]" "" : split-path-VMS "disk:[dir]" ; 774# assert.result "disk:" "" "" : split-path-VMS "disk:" ; 775# assert.result "disk:" "" "file" : split-path-VMS "disk:file" ; 776# assert.result "" "[dir]" "file" : split-path-VMS "[dir]file" ; 777# assert.result "" "[dir]" "" : split-path-VMS "[dir]" ; 778# assert.result "" "" "file" : split-path-VMS "file" ; 779# assert.result "" "" "" : split-path-VMS "" ; 780# 781# # 782# # Special case: current directory 783# # 784# assert.result "" "[]" "" : split-path-VMS "[]" ; 785# assert.result "disk:" "[]" "" : split-path-VMS "disk:[]" ; 786# assert.result "" "[]" "file" : split-path-VMS "[]file" ; 787# assert.result "disk:" "[]" "file" : split-path-VMS "disk:[]file" ; 788# 789# # 790# # Make portable paths 791# # 792# assert.result "/disk:" : make "disk:" ; 793# assert.result "foo/bar/giz" : make "[.foo.bar.giz]" ; 794# assert.result "foo" : make "[.foo]" ; 795# assert.result "foo" : make "[.foo.bar.-]" ; 796# assert.result ".." : make "[.-]" ; 797# assert.result ".." : make "[-]" ; 798# assert.result "." : make "[]" ; 799# assert.result "giz.h" : make "giz.h" ; 800# assert.result "foo/bar/giz.h" : make "[.foo.bar]giz.h" ; 801# assert.result "/disk:/my_docs" : make "disk:[my_docs]" ; 802# assert.result "/disk:/boost/tools/build/new/project.jam" : make "disk:[boost.tools.build.test.-.new]project.jam" ; 803# 804# # 805# # Special case (adds '.' to end of file w/o extension to 806# # disambiguate from directory in portable path spec). 807# # 808# assert.result "Jamfile." : make "Jamfile" ; 809# assert.result "dir/Jamfile." : make "[.dir]Jamfile" ; 810# assert.result "/disk:/dir/Jamfile." : make "disk:[dir]Jamfile" ; 811# 812# # 813# # Make native paths 814# # 815# assert.result "disk:" : native "/disk:" ; 816# assert.result "[.foo.bar.giz]" : native "foo/bar/giz" ; 817# assert.result "[.foo]" : native "foo" ; 818# assert.result "[.-]" : native ".." ; 819# assert.result "[.foo.-]" : native "foo/.." ; 820# assert.result "[]" : native "." ; 821# assert.result "disk:[my_docs.work]" : native "/disk:/my_docs/work" ; 822# assert.result "giz.h" : native "giz.h" ; 823# assert.result "disk:Jamfile." : native "/disk:Jamfile." ; 824# assert.result "disk:[my_docs.work]Jamfile." : native "/disk:/my_docs/work/Jamfile." ; 825# 826# modules.poke path : os : $(save-os) ; 827# 828# } 829 830# 831 832 833#def glob(dir, patterns): 834# result = [] 835# for pattern in patterns: 836# result.extend(builtin_glob(os.path.join(dir, pattern))) 837# return result 838 839def glob(dirs, patterns, exclude_patterns=None): 840 """Returns the list of files matching the given pattern in the 841 specified directory. Both directories and patterns are 842 supplied as portable paths. Each pattern should be non-absolute 843 path, and can't contain '.' or '..' elements. Each slash separated 844 element of pattern can contain the following special characters: 845 - '?', which match any character 846 - '*', which matches arbitrary number of characters. 847 A file $(d)/e1/e2/e3 (where 'd' is in $(dirs)) matches pattern p1/p2/p3 848 if and only if e1 matches p1, e2 matches p2 and so on. 849 For example: 850 [ glob . : *.cpp ] 851 [ glob . : */build/Jamfile ] 852 """ 853 854 assert(isinstance(patterns, list)) 855 assert(isinstance(dirs, list)) 856 857 if not exclude_patterns: 858 exclude_patterns = [] 859 else: 860 assert(isinstance(exclude_patterns, list)) 861 862 real_patterns = [os.path.join(d, p) for p in patterns for d in dirs] 863 real_exclude_patterns = [os.path.join(d, p) for p in exclude_patterns 864 for d in dirs] 865 866 inc = [os.path.normpath(name) for p in real_patterns 867 for name in builtin_glob(p)] 868 exc = [os.path.normpath(name) for p in real_exclude_patterns 869 for name in builtin_glob(p)] 870 return [x for x in inc if x not in exc] 871 872def glob_tree(roots, patterns, exclude_patterns=None): 873 """Recursive version of GLOB. Builds the glob of files while 874 also searching in the subdirectories of the given roots. An 875 optional set of exclusion patterns will filter out the 876 matching entries from the result. The exclusions also apply 877 to the subdirectory scanning, such that directories that 878 match the exclusion patterns will not be searched.""" 879 880 if not exclude_patterns: 881 exclude_patterns = [] 882 883 result = glob(roots, patterns, exclude_patterns) 884 subdirs = [s for s in glob(roots, ["*"], exclude_patterns) if s != "." and s != ".." and os.path.isdir(s)] 885 if subdirs: 886 result.extend(glob_tree(subdirs, patterns, exclude_patterns)) 887 888 return result 889 890def glob_in_parents(dir, patterns, upper_limit=None): 891 """Recursive version of GLOB which glob sall parent directories 892 of dir until the first match is found. Returns an empty result if no match 893 is found""" 894 895 assert(isinstance(dir, str)) 896 assert(isinstance(patterns, list)) 897 898 result = [] 899 900 absolute_dir = os.path.join(os.getcwd(), dir) 901 absolute_dir = os.path.normpath(absolute_dir) 902 while absolute_dir: 903 new_dir = os.path.split(absolute_dir)[0] 904 if new_dir == absolute_dir: 905 break 906 result = glob([new_dir], patterns) 907 if result: 908 break 909 absolute_dir = new_dir 910 911 return result 912 913 914# The relpath functionality is written by 915# Cimarron Taylor 916def split(p, rest=[]): 917 (h,t) = os.path.split(p) 918 if len(h) < 1: return [t]+rest 919 if len(t) < 1: return [h]+rest 920 return split(h,[t]+rest) 921 922def commonpath(l1, l2, common=[]): 923 if len(l1) < 1: return (common, l1, l2) 924 if len(l2) < 1: return (common, l1, l2) 925 if l1[0] != l2[0]: return (common, l1, l2) 926 return commonpath(l1[1:], l2[1:], common+[l1[0]]) 927 928def relpath(p1, p2): 929 (common,l1,l2) = commonpath(split(p1), split(p2)) 930 p = [] 931 if len(l1) > 0: 932 p = [ '../' * len(l1) ] 933 p = p + l2 934 if p: 935 return os.path.join( *p ) 936 else: 937 return "." 938