1# Copyright 2021 The Cirq Developers 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import os 16import sys 17 18 19def explode(req: str): 20 """Returns the exploded dependency list for a requirements file. 21 22 As requirements files can include other requirements files with the -r directive, it can be 23 useful to see a flattened version of all the constraints. This method unrolls a requirement file 24 and produces a list of strings for each constraint line in the order of inclusion. 25 26 Args: 27 req: path to a requirements file. 28 Returns: 29 list of lines of requirements 30 """ 31 res = [] 32 d = os.path.dirname(req) 33 with open(req) as f: 34 for l in f.readlines(): 35 l = l.rstrip("\n") 36 l = l.lstrip(" ") 37 if l.startswith("-r"): 38 include = l.lstrip(" ").lstrip("-r").lstrip(" ") 39 # assuming relative includes always 40 res += explode(os.path.join(d, include)) 41 elif l: 42 res += [l] 43 return res 44 45 46if __name__ == '__main__': 47 print('\n'.join(explode(sys.argv[1]))) 48