├── .appveyor.yml ├── .codecov.yml ├── .coveragerc ├── .gitignore ├── .landscape.yml ├── .style.yapf ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.rst ├── conftest.py ├── cppdep ├── __init__.py ├── __main__.py ├── config_schema.yml ├── cppdep.py └── graph.py ├── logo.png ├── requirements-dev.txt ├── requirements-test.txt ├── requirements.txt ├── setup.py └── test ├── test_cppdep.py └── test_graph.py /.appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | matrix: 3 | - PYTHON: Python27 4 | - PYTHON: Python34 5 | - PYTHON: Python35 6 | 7 | install: 8 | - C:\%PYTHON%\Scripts\pip install -r requirements.txt -r requirements-test.txt 9 | 10 | build: off 11 | 12 | test_script: 13 | - C:\%PYTHON%\Scripts\pytest 14 | -------------------------------------------------------------------------------- /.codecov.yml: -------------------------------------------------------------------------------- 1 | coverage: 2 | ignore: 3 | - test/* 4 | - setup.py 5 | - conftest.py 6 | -------------------------------------------------------------------------------- /.coveragerc: -------------------------------------------------------------------------------- 1 | [run] 2 | branch = True 3 | source = ./ 4 | 5 | [report] 6 | exclude_lines = 7 | if self.debug: 8 | pragma: no cover 9 | raise NotImplementedError 10 | if __name__ == .__main__.: 11 | ignore_errors = True 12 | omit = 13 | test/* 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | .cache 3 | .coverage 4 | htmlcov 5 | dist 6 | *.egg-info 7 | -------------------------------------------------------------------------------- /.landscape.yml: -------------------------------------------------------------------------------- 1 | doc-warnings: yes 2 | test-warnings: yes 3 | strictness: veryhigh 4 | max-line-length: 80 5 | pep257: 6 | disable: [D406, D413, D407, D213, D202, D203, D401] 7 | pep8: 8 | full: true 9 | mccabe: 10 | options: {max-complexity: 15} 11 | pylint: 12 | enable: [bad-continuation, fixme] 13 | options: {max-nested-blocks: 3} 14 | python-targets: 15 | - 2 16 | - 3 17 | -------------------------------------------------------------------------------- /.style.yapf: -------------------------------------------------------------------------------- 1 | [style] 2 | based_on_style = google 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - 2.7 4 | - 3.4 5 | - 3.5 6 | - pypy 7 | - pypy3 8 | 9 | os: linux 10 | 11 | install: 12 | - if [[ "${TRAVIS_PYTHON_VERSION}" == "2.7" ]]; then 13 | export RUN_COVERAGE=true; 14 | pip install coverage codecov pytest-cov; 15 | pip install yapf; 16 | fi 17 | - pip install -r requirements.txt 18 | 19 | script: 20 | - if [[ $RUN_COVERAGE == true ]]; then 21 | python -m pytest --cov=cppdep --cov=graph --cov-config .coveragerc && codecov; 22 | yapf -d cppdep/*.py; 23 | else 24 | python -m pytest; 25 | fi 26 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](http://keepachangelog.com/) 5 | and this project adheres to [Semantic Versioning](http://semver.org/) 6 | 7 | 8 | ## [Unreleased] 9 | 10 | ## [0.2.4] - 2017-10-24 11 | ### Fixed 12 | - Add pydot as dependency (#41) 13 | 14 | ## [0.2.3] - 2017-09-26 15 | ### Fixed 16 | - Adjust for NetworkX 2.0 17 | 18 | ## [0.2.2] - 2017-02-14 19 | ### Fixed 20 | - cppdep running twice per call after installation with setup.py (#39) 21 | 22 | ## [0.2.1] - 2017-02-04 23 | ### Changed 24 | - Allow '-' in source file names 25 | - Move the example configuration to project wiki 26 | 27 | ## Fixed 28 | - PyPI installation failure due to project structure (#38) 29 | 30 | ## [0.2.0] - 2017-02-02 31 | ### Added 32 | - Pairing header and implementation files in different locations (#19) 33 | - Handle 'ipp' template implementation source files (#31) 34 | - Behavior specification for anomalous conflicting component files (#27) 35 | - Implement ignore/exclude paths (#23) 36 | - Accept glob pattern for source paths (#36) 37 | - Project wiki pages 38 | - Regex pattern based include directive classification (#22) 39 | - Deduce external packages from the include directive w/o filesystem search (#18) 40 | - Handle header files w/o extensions (Boost/STL/Qt/etc.) (#32) 41 | - Use POSIX path separator in component names (for cross-platform report stability) 42 | - Configuration file validation against the schema (with PyKwalify) 43 | 44 | ### Changed 45 | - pytest instead of nose 46 | - YAML configuration files instead of XML (#24) 47 | 48 | ### Removed 49 | - Implicit single-path alias Package construction 50 | 51 | ### Fixed 52 | - Exception leaks out of main() 53 | - Unicode Escape Error on graph dot on Windows with Python 2.7 (#35) 54 | - Python3 UnicodeDecodeError for 'utf-8' in source files (#30) 55 | - Logging: Type Error: not all arguments converted during string formatting (#28) 56 | 57 | ## [0.1.0] - 2017-01-05 58 | ### Added 59 | - The original ldep '-l|-L' options to print dependencies (#20) 60 | - '-o' to print reports into a file 61 | - Warn about duplicate and redundant includes (#13) 62 | - Extended definition for 'Component' (#7) 63 | - PEP-257 conformance (#2) 64 | - PEP-8 conformance (#1) 65 | - Python 3 support 66 | - PyPI package 67 | - XML configuration example and RNG schema 68 | - Travis CI (Linux, OS X) and AppVeyor CI (Windows) setups 69 | 70 | ### Changed 71 | - Differentiate 'paths' into source, include, and alias. 72 | - Print warnings to stderr instead of stdout (#12) 73 | - Report Component levels instead of Graph layers (#9) 74 | - Refactor the procedural design into the object-oriented design (#4) 75 | - Change '-f' flag into '-c' flag 76 | - Replace optparse with argparse 77 | - XML configuration file format 78 | 79 | ### Removed 80 | - Redundant printing a list of cumulative dependencies (#20) 81 | - Indirect missing-header include warnings 82 | - Global cross-package and cross-package-group component dependency analysis 83 | - 'details-of-components/--debug' verbosity 84 | - ``dot2any.py`` helper script 85 | - Manual profiling code (use ``pyvmmonitor`` instead) 86 | - Manual testing code (automated with ``nosetest``) 87 | 88 | ### Fixed 89 | - Level 0 External components missing from the report and graph (#21) 90 | - Incorrect dependency processing with file basenames (#6) 91 | - Wrong level calculation for cycles (#8) 92 | - Double counting of common components in CCD calculations (#11) 93 | - Missing cycles from the Dot graph (#10) 94 | - Outdated networkx API usage 95 | 96 | 97 | ## [0.0.0] - 2016-09-24 98 | Big Bang: fork https://github.com/yuzhichang/cppdep 99 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | ###### 2 | |logo| 3 | ###### 4 | 5 | .. image:: https://travis-ci.org/rakhimov/cppdep.svg?branch=master 6 | :target: https://travis-ci.org/rakhimov/cppdep 7 | .. image:: https://ci.appveyor.com/api/projects/status/1ff39sfjp7ija3j8/branch/master?svg=true 8 | :target: https://ci.appveyor.com/project/rakhimov/cppdep/branch/master 9 | :alt: 'Build status' 10 | .. image:: https://codecov.io/gh/rakhimov/cppdep/branch/master/graph/badge.svg 11 | :target: https://codecov.io/gh/rakhimov/cppdep 12 | .. image:: https://landscape.io/github/rakhimov/cppdep/master/landscape.svg?style=flat 13 | :target: https://landscape.io/github/rakhimov/cppdep/master 14 | :alt: Code Health 15 | .. image:: https://badge.fury.io/py/cppdep.svg 16 | :target: https://badge.fury.io/py/cppdep 17 | 18 | | 19 | 20 | ``cppdep`` performs dependency analysis 21 | among components/packages/package groups of a large C/C++ project. 22 | This is a rewrite of ``dep_utils(adep/cdep/ldep)``, 23 | which is provided by John Lakos' book 24 | "Large-Scale C++ Software Design", Addison Wesley (1996). 25 | 26 | .. |logo| image:: logo.png 27 | 28 | 29 | Limitations 30 | =========== 31 | 32 | - Indirect `extern` declarations of global variables or functions 33 | instead of including the proper component header with the declarations. 34 | - Embedded dynamic dependencies, 35 | such as dynamic loading and configurable internal services. 36 | - Preprocessing or macro expansion is not performed. 37 | Dependency inclusion via preprocessor *meta-programming* is not handled. 38 | - Dependency exclusion with C style multi-line comments or macros 39 | is not respected. 40 | 41 | 42 | Requirements 43 | ============ 44 | 45 | #. Python 2.7 or 3.4+ 46 | #. `NetworkX `_ 47 | #. pydot 48 | #. pydotplus 49 | #. PyYAML 50 | #. PyKwalify 1.6.0+ 51 | 52 | The dependencies can be installed with ``pip``. 53 | 54 | .. code-block:: bash 55 | 56 | $ sudo pip install -r requirements.txt 57 | 58 | 59 | Installation 60 | ============ 61 | 62 | From the source: 63 | 64 | .. code-block:: bash 65 | 66 | $ ./setup.py install 67 | 68 | The latest stable release from PyPi: 69 | 70 | .. code-block:: bash 71 | 72 | $ pip install cppdep 73 | 74 | 75 | Usage 76 | ===== 77 | 78 | Create a configuration file 79 | that describes the project for analysis. 80 | ``config_schema.yml`` is given for guidance. 81 | 82 | In the root directory of the project with the configuration file, 83 | run the following command to generate dependency analysis reports and graphs. 84 | 85 | .. code-block:: bash 86 | 87 | $ cppdep -c /path/to/config/file 88 | 89 | More documentation and example configurations 90 | can be found in project `wiki `_. 91 | 92 | 93 | Acknowledgments 94 | =============== 95 | 96 | - John Lakos for inventing the analysis and providing ``dep_utils``. 97 | - `Zhichang Yu `_ for rewriting ``dep_utils`` into Python. 98 | -------------------------------------------------------------------------------- /conftest.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2017 Olzhas Rakhimov 2 | # 3 | # This program is free software; you can redistribute it and/or modify 4 | # it under the terms of the GNU General Public License as published by 5 | # the Free Software Foundation; either version 3 of the License, or 6 | # (at your option) any later version. 7 | # 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU General Public License for more details. 12 | # 13 | # You should have received a copy of the GNU General Public License 14 | # along with this program. If not, see . 15 | """Configuration facilities for cppdep tests with pytest.""" 16 | 17 | from cppdep.cppdep import Include 18 | 19 | 20 | #pylint: disable=invalid-name 21 | def pytest_assertrepr_compare(op, left, right): 22 | """Custom assertion messages for cppdep classes.""" 23 | if isinstance(left, Include) and isinstance(right, Include): 24 | if op in ('==', '!='): 25 | return [ 26 | 'Comparing Include directives:', 27 | ' vals: %s %s %s' % (str(left), { 28 | '==': '!=', 29 | '!=': '==' 30 | }[op], str(right)) 31 | ] 32 | -------------------------------------------------------------------------------- /cppdep/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rakhimov/cppdep/307ba5838a2b5a44c661b41b9be7a8e91b002849/cppdep/__init__.py -------------------------------------------------------------------------------- /cppdep/__main__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # Copyright (C) 2016-2017 Olzhas Rakhimov 4 | # Copyright (C) 2010, 2014 Zhichang Yu 5 | # 6 | # This program is free software; you can redistribute it and/or modify 7 | # it under the terms of the GNU General Public License as published by 8 | # the Free Software Foundation; either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # This program is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU General Public License 17 | # along with this program. If not, see . 18 | """The command-line entry point for the package.""" 19 | 20 | from __future__ import print_function, absolute_import 21 | 22 | import argparse as ap 23 | import logging 24 | import sys 25 | 26 | from yaml import YAMLError 27 | from pykwalify.core import SchemaError 28 | 29 | from cppdep import cppdep 30 | 31 | 32 | def main(argv=None): 33 | """Runs the dependency analysis and prints results and graphs.""" 34 | parser = ap.ArgumentParser(description=cppdep.__doc__, 35 | formatter_class=ap.ArgumentDefaultsHelpFormatter) 36 | parser.add_argument('--version', 37 | action='store_true', 38 | default=False, 39 | help='show the version information and exit') 40 | parser.add_argument( 41 | '-c', 42 | '--config', 43 | default='.cppdep.yml', 44 | help='a YAML file describing the C/C++ project structure') 45 | parser.add_argument('-l', 46 | action='store_true', 47 | default=False, 48 | help='list reduced dependencies of nodes') 49 | parser.add_argument('-L', 50 | action='store_true', 51 | default=False, 52 | help='list unreduced dependencies of nodes') 53 | parser.add_argument('-o', '--output', metavar='path', help='output file') 54 | args = parser.parse_args(argv) 55 | if args.version: 56 | print(cppdep.VERSION) 57 | return 58 | 59 | def _die(head, body): 60 | logging.error(str('%s:\n%s' % (head, str(body)))) 61 | sys.exit(1) 62 | 63 | try: 64 | analysis = cppdep.DependencyAnalysis(args.config) 65 | printer = get_printer(args.output) 66 | analysis.analyze(printer, args) 67 | except IOError as err: 68 | _die('IO Error', err) 69 | except YAMLError as err: 70 | _die('Malformed Configuration File', err) 71 | except SchemaError as err: 72 | _die('Configuration File Validity Error', err) 73 | except cppdep.InvalidArgumentError as err: 74 | _die('Invalid Argument Error', err) 75 | except cppdep.AnalysisError as err: 76 | _die('Analysis (Configuration) Error', err) 77 | 78 | 79 | def get_printer(file_path=None): 80 | """Returns printer for the report.""" 81 | destination = open(file_path, 'w') if file_path else sys.stdout 82 | 83 | def _print(*args): 84 | print(*args, file=destination) 85 | 86 | return _print 87 | 88 | 89 | if __name__ == "__main__": 90 | main() 91 | -------------------------------------------------------------------------------- /cppdep/config_schema.yml: -------------------------------------------------------------------------------- 1 | # Note that 'map' and 'seq' are implicit types. 2 | map: 3 | internal: # A list of package groups for analysis. 4 | required: True 5 | seq: &Groups 6 | - map: 7 | name: # The unique name of a package group. 8 | required: True 9 | type: str 10 | path: # The root path for the packages in the group. 11 | required: True 12 | type: str 13 | packages: # A list of member packages. 14 | required: True 15 | seq: # Note: all paths are relative to the group path. 16 | - map: 17 | name: # Unique package name within the group. 18 | required: True 19 | type: str 20 | src: # Paths to source files to analyze. 21 | seq: 22 | - type: str # glob pattern 23 | ignore: # Paths to exclude from the source. 24 | seq: 25 | - type: str # glob pattern 26 | include: # Header search paths, i.e., '-i'. 27 | seq: 28 | - type: str 29 | alias: # Alias paths to map to a name upon include. 30 | seq: 31 | - type: str 32 | pattern: # Include processing w/o header search. 33 | seq: # Meaningful only for external packages. 34 | - type: str # Regex pattern for include. 35 | external: # External package groups (not analyzed but searched for headers/components). 36 | seq: *Groups 37 | -------------------------------------------------------------------------------- /cppdep/cppdep.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2016-2017 Olzhas Rakhimov 2 | # Copyright (C) 2010, 2014 Zhichang Yu 3 | # 4 | # This program is free software; you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation; either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with this program. If not, see . 16 | """C/C++ dependency analyzer. 17 | 18 | Physical dependency analyzer 19 | for components/packages/package groups of a large C/C++ project. 20 | """ 21 | 22 | from __future__ import absolute_import 23 | 24 | import collections 25 | import fnmatch 26 | import glob 27 | import itertools 28 | import logging 29 | import os.path 30 | import re 31 | import sys 32 | 33 | from yaml import safe_load 34 | from pykwalify.core import Core as Validator 35 | 36 | from .graph import Graph 37 | 38 | VERSION = '0.2.4' # The latest release version. 39 | 40 | _SCHEMA_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 41 | 'config_schema.yml') 42 | assert os.path.isfile(_SCHEMA_FILE), 'The cppdep schema file is missing.' 43 | assert safe_load(open(_SCHEMA_FILE)) # Will throw if invalid. 44 | 45 | _FILE_OPEN_FLAGS = {} if sys.version[0] == '2' else {'errors': 'replace'} 46 | 47 | # Allowed common abbreviations in the code: 48 | # ccd - Cumulative Component Dependency (CCD) 49 | # nccd - Normalized CCD 50 | # accd - Average CCD 51 | # cd - component dependency (discouraged abbreviation!) 52 | # pkg - package (discouraged abbreviation!) 53 | # hfile - header file 54 | # cfile - implementation file 55 | # dep - dependency (discouraged abbreviation!) 56 | 57 | 58 | class InvalidArgumentError(Exception): 59 | """General errors with invalid arguments.""" 60 | 61 | pass 62 | 63 | 64 | class AnalysisError(Exception): 65 | """The analysis cannot complete due to misconfiguration.""" 66 | 67 | pass 68 | 69 | 70 | def warn(message): 71 | """Logs a warning message.""" 72 | logging.warn(message) 73 | 74 | 75 | def strip_ext(filename): 76 | """Strips the extension from a filename.""" 77 | return os.path.splitext(filename)[0] 78 | 79 | 80 | def path_normjoin(path, *paths): 81 | """Returns normalized result of joining of paths.""" 82 | return os.path.normpath(os.path.join(path, *paths)) 83 | 84 | 85 | def path_common(paths): 86 | """Returns common prefix path for the argument absolute normalized paths.""" 87 | if not paths: 88 | return '' 89 | path = os.path.commonprefix(paths) 90 | assert os.path.isabs(path) 91 | if path[-1] == os.path.sep: 92 | return os.path.dirname(path) 93 | sep_pos = len(path) 94 | if all(len(x) == sep_pos or x[sep_pos] == os.path.sep for x in paths): 95 | return path 96 | return os.path.dirname(path) 97 | 98 | 99 | def path_isancestor(parent, child): 100 | """Returns true if the child abspath is a subpath of the parent abspath.""" 101 | if len(parent) > len(child) or not child.startswith(parent): 102 | return False 103 | return (len(parent) == len(child) or parent[-1] == os.path.sep or 104 | child[len(parent)] == os.path.sep) 105 | 106 | 107 | def path_to_posix_sep(path): 108 | """Normalize the path separator to Posix (mostly for Windows).""" 109 | return path.replace('\\', '/') if os.name == 'nt' else path 110 | 111 | 112 | def yaml_optional(dictionary, element, default_value): 113 | """Retrieves optional element values with defaults.""" 114 | return dictionary[element] if element in dictionary else default_value 115 | 116 | 117 | def yaml_optional_list(dictionary, element): 118 | """Retrieves optional list values with an empty list as default.""" 119 | return yaml_optional(dictionary, element, []) 120 | 121 | 122 | class Include(object): 123 | """Representation of an include directive. 124 | 125 | Attributes: 126 | with_quotes: True if the include is within quotes ("") 127 | instead of angle brackets (<>). 128 | hfile: The normalized path to the header file in the directive. 129 | hpath: The absolute path to the header file. 130 | """ 131 | 132 | _RE_INCLUDE = re.compile(r'^\s*#\s*include\s*' 133 | r'(<(?P\S+?)>|"(?P\S+?)")') 134 | 135 | __slots__ = ['__include_path', 'hfile', 'with_quotes', 'hpath'] 136 | 137 | def __init__(self, include_path, with_quotes): 138 | """Initializes with attributes. 139 | 140 | Args: 141 | include_path: The original path in the include directive. 142 | with_quotes: True if the path is within quotes instead of brackets. 143 | """ 144 | self.__include_path = include_path 145 | self.hfile = os.path.normpath(include_path) 146 | self.with_quotes = with_quotes 147 | self.hpath = None 148 | 149 | def __str__(self): 150 | """Produces the original include with quotes or brackets.""" 151 | if self.with_quotes: 152 | return '"%s"' % self.__include_path 153 | return '<%s>' % self.__include_path 154 | 155 | def __hash__(self): 156 | """To work with sets.""" 157 | return hash(self.hfile) 158 | 159 | def __eq__(self, other): 160 | """Assumes the same working directory and search paths.""" 161 | return self.hfile == other.hfile 162 | 163 | def __ne__(self, other): 164 | """Assumes the same working directory and search paths.""" 165 | return not self == other 166 | 167 | @staticmethod 168 | def grep(file_path): 169 | """Processes include directives in a source file. 170 | 171 | Args: 172 | file_path: The full path to the source file. 173 | 174 | Yields: 175 | Include objects constructed with the directives. 176 | """ 177 | with open(file_path, **_FILE_OPEN_FLAGS) as src_file: 178 | for line in src_file: 179 | include = Include._RE_INCLUDE.search(line) 180 | if not include: 181 | continue 182 | if include.group("brackets"): 183 | yield Include(include.group("brackets"), with_quotes=False) 184 | else: 185 | yield Include(include.group("quotes"), with_quotes=True) 186 | 187 | def locate(self, cwd, include_dirs, include_patterns): 188 | """Locates the included header file path. 189 | 190 | All input directory paths must be absolute. 191 | 192 | Args: 193 | cwd: The working directory for source file processing. 194 | include_dirs: The directories to search for the file, 195 | ordered from internal to external/system directories. 196 | include_patterns: (package, [regex]) to search with patterns. 197 | 198 | Returns: 199 | (hpath, package) with None indicating failure to find the file. 200 | """ 201 | assert self.hpath is None 202 | 203 | def _find_in(include_dir): 204 | """Returns True if the path is found.""" 205 | file_hpath = path_normjoin(include_dir, self.hfile) 206 | if os.path.isfile(file_hpath): 207 | self.hpath = file_hpath 208 | return True 209 | return False 210 | 211 | if self.with_quotes and _find_in(cwd): 212 | return self.hpath, None 213 | 214 | for package, patterns in include_patterns: 215 | if any(x.match(self.hfile) for x in patterns): 216 | return self.hfile, package 217 | 218 | direction = iter if self.with_quotes else reversed 219 | if any(_find_in(x) for x in direction(include_dirs)): 220 | return self.hpath, None 221 | 222 | return None, None 223 | 224 | 225 | class Component(object): 226 | """Representation of a component in a package. 227 | 228 | Attributes: 229 | name: A unique name within the package. 230 | hpath: The absolute path to the header file. 231 | cpath: The absolute path to the implementation file. 232 | package: The package this component belongs to. 233 | working_dir: The parent directory. 234 | dep_components: Dependency components. 235 | includes_in_h: Include directives in the header file. 236 | includes_in_c: Include directives in the implementation file. 237 | """ 238 | 239 | def __init__(self, hpath, cpath, package): 240 | """Initialization of a free-standing component. 241 | 242 | Warns about incomplete components. 243 | 244 | Args: 245 | hpath: The path to the header file of the component. 246 | cpath: The path to the implementation file of the component. 247 | package: The package this components belongs to. 248 | """ 249 | assert hpath or cpath 250 | self.name = path_to_posix_sep( 251 | strip_ext(os.path.relpath(cpath or hpath, package.root))) 252 | if not hpath: 253 | warn('incomplete component: missing header: %s in %s.%s' % 254 | (self.name, package.group.name, package.name)) 255 | self.hpath = hpath 256 | self.cpath = cpath 257 | self.package = package 258 | self.working_dir = os.path.dirname(cpath or hpath) 259 | self.dep_components = set() 260 | self.includes_in_h = set() if not hpath else list(Include.grep(hpath)) 261 | self.includes_in_c = set() if not cpath else list(Include.grep(cpath)) 262 | self.__sanitize_includes() 263 | 264 | def __str__(self): 265 | """For printing graph nodes.""" 266 | return self.name 267 | 268 | def dependencies(self): 269 | """Returns dependency components.""" 270 | return self.dep_components 271 | 272 | def __sanitize_includes(self): 273 | """Sanitizes and checks includes.""" 274 | 275 | def _check_duplicates(path, includes): 276 | unique_includes = set() 277 | for include in includes: 278 | if include in unique_includes: 279 | warn('include issues: duplicate include: ' 280 | '%s in %s' % (str(include), path)) 281 | else: 282 | unique_includes.add(include) 283 | return unique_includes 284 | 285 | def _remove_duplicates(): 286 | if self.hpath: 287 | self.includes_in_h = _check_duplicates(self.hpath, 288 | self.includes_in_h) 289 | if self.cpath: 290 | self.includes_in_c = _check_duplicates(self.cpath, 291 | self.includes_in_c) 292 | 293 | def _remove_redundant(): 294 | for include in self.includes_in_c: 295 | if include in self.includes_in_h: 296 | warn('include issues: redundant include: ' 297 | '%s in %s' % (str(include), self.cpath)) 298 | self.includes_in_c.difference_update(self.includes_in_h) 299 | 300 | if self.hpath and self.cpath: 301 | hfile = os.path.basename(self.hpath) 302 | if hfile not in ( 303 | os.path.basename(x.hfile) for x in self.includes_in_c): 304 | warn('include issues: missing include: ' 305 | '%s does not include %s.' % (self.cpath, hfile)) 306 | elif hfile != os.path.basename(self.includes_in_c[0].hfile): 307 | warn('include issues: include order: ' 308 | '%s should be the first include in %s.' % 309 | (hfile, self.cpath)) 310 | _remove_duplicates() 311 | _remove_redundant() 312 | 313 | 314 | class ExternalComponent(object): 315 | """Representation of an external component. 316 | 317 | Note that external components are degenerate. 318 | There's no need to acquire full information about their dependencies. 319 | 320 | Attributes: 321 | hpath: A path to the component header as an identifier. 322 | package: The package. 323 | """ 324 | 325 | __slots__ = ['hpath', 'package'] 326 | 327 | def __init__(self, hpath, package): 328 | """Constructs an external component with its attributes.""" 329 | self.hpath = hpath 330 | self.package = package 331 | 332 | 333 | class Package(object): 334 | """A collection of components. 335 | 336 | Attributes: 337 | name: The unique identifier name of the package within its group. 338 | src_paths: The absolute directory paths the package source components. 339 | include_paths: The export paths of the package headers. 340 | alias_paths: The absolute directory paths aliasing to this package. 341 | group: The package group this package belongs to. 342 | root: The common root path for all the paths in the package. 343 | components: The list of unique components in this package. 344 | """ 345 | 346 | _RE_SRC = re.compile(r'(?i)[\w\-]+((?P(\.h(h|xx|\+\+|pp)?)?)|' 347 | r'(?P\.((c(c|xx|\+\+|pp)?)|ipp)))$') 348 | 349 | def __init__(self, name, group, src_paths, include_paths, alias_paths, 350 | include_patterns, ignore_paths): 351 | """Constructs an empty package. 352 | 353 | Registers the package in the package group. 354 | The argument paths are relative to the package group directory. 355 | 356 | Args: 357 | name: A unique identifier within the package group. 358 | group: The package group. 359 | src_paths: The source directory paths (glob patterns). 360 | include_paths: The export header paths (also alias paths). 361 | alias_paths: Additional directory paths aliasing to the package. 362 | include_patterns: Regex pattern strings for include directives. 363 | ignore_paths: Exlusion paths from the source (glob patterns). 364 | 365 | Raises: 366 | InvalidArgumentError: Issues with the argument directory paths. 367 | """ 368 | self.name = name 369 | self.group = group 370 | self.src_paths = set() 371 | self.include_paths = set() 372 | self.ignore_paths = set() 373 | self.alias_paths = set() 374 | self.include_patterns = include_patterns 375 | self.__init_paths(src_paths, include_paths, alias_paths, ignore_paths) 376 | self.root = path_common(self.src_paths) 377 | self.components = [] 378 | self.__dep_packages = None # set of dependency packages 379 | group.add_package(self) 380 | 381 | def __str__(self): 382 | """For printing graph nodes.""" 383 | return self.name 384 | 385 | def __init_paths(self, src_paths, include_paths, alias_paths, ignore_paths): 386 | """Initializes package paths.""" 387 | 388 | def _update(path_container, arg_paths, check_dir=True): 389 | for path in arg_paths: 390 | path = os.path.normpath(path) 391 | abs_path = path_normjoin(self.group.path, path) 392 | if (check_dir and not os.path.isdir(abs_path) or 393 | not abs_path.startswith(self.group.path)): 394 | raise InvalidArgumentError( 395 | '%s is not a directory in %s (group %s).' % 396 | (path, self.group.path, self.group.name)) 397 | if abs_path in path_container: 398 | raise InvalidArgumentError( 399 | '%s is duplicated in %s.%s' % 400 | (abs_path, self.group.name, self.name)) 401 | path_container.add(abs_path) 402 | 403 | _update(self.src_paths, src_paths, check_dir=False) 404 | _update(self.ignore_paths, ignore_paths, check_dir=False) 405 | _update(self.include_paths, include_paths) 406 | _update(self.alias_paths, alias_paths) 407 | self.alias_paths.update(self.include_paths) 408 | 409 | def construct_components(self): 410 | """Traverses the package paths and constructs package components. 411 | 412 | Even though John Lakos defined a component as a pair of h and c files, 413 | C++ can have template only components 414 | residing only in header files (e.g., STL/Boost/etc.). 415 | Moreover, some header-only components 416 | may contain only inline functions or macros 417 | without any need for an implementation file 418 | (e.g., inline math, Boost PPL). 419 | For these reasons, unpaired header files 420 | are counted as components by default. 421 | 422 | Unpaired c files are counted as incomplete components with warnings. 423 | """ 424 | file_type = collections.namedtuple('File', ['rev_path', 'path']) 425 | hpaths = collections.defaultdict(list) 426 | cpaths = collections.defaultdict(list) 427 | 428 | # This approach is pessimistic with O(N*logN) instead of O(N) 429 | # because it assumes the header and implementation files 430 | # are likely to be in different directories. 431 | def _reverse(path): 432 | path = strip_ext(path).split(os.path.sep) 433 | path.reverse() 434 | return path 435 | 436 | def _select_src_file(root, filename): 437 | full_path = os.path.join(root, filename) 438 | if any(fnmatch.fnmatch(full_path, x) for x in self.ignore_paths): 439 | return 440 | src_match = Package._RE_SRC.match(filename) 441 | if src_match: 442 | src_container = hpaths if src_match.group('h') else cpaths 443 | src_container[strip_ext(filename)].append( 444 | file_type(_reverse(full_path), full_path)) 445 | 446 | def _gather_files(dir_path): 447 | for root, _, files in os.walk(dir_path): 448 | if any(fnmatch.fnmatch(root, x) for x in self.ignore_paths): 449 | continue 450 | for filename in files: 451 | _select_src_file(root, filename) 452 | 453 | for glob_path in self.src_paths: 454 | for src_path in glob.iglob(glob_path): 455 | if os.path.isdir(src_path): 456 | _gather_files(src_path) 457 | else: 458 | _select_src_file(*os.path.split(src_path)) 459 | 460 | self.__pair_files(hpaths, cpaths) 461 | 462 | def __pair_files(self, hpaths, cpaths): 463 | """Pairs header and implementation files into components.""" 464 | 465 | # This should probably be solved with a graph algorithm. 466 | # Find the nodes with the longest matching consecutive ancestors 467 | # starting from the node (not the root!). 468 | # The nodes represent the file and directory names. 469 | # 470 | # The association is indeterminate or ambiguous 471 | # if multiple nodes share the same common ancestors of the same number. 472 | # Therefore, the algorithm to find 473 | # the lowest common ancestor seems to lead to false answers. 474 | def _num_consecutive_ancestors(file_one, file_two): 475 | return sum( 476 | 1 for _ in 477 | itertools.takewhile(lambda x: x[0] == x[1], 478 | zip(file_one.rev_path, file_two.rev_path))) 479 | 480 | def _pair(hfiles, cfiles): 481 | assert hfiles and cfiles # Expected to have few elements. 482 | candidates = [(x, 483 | sorted(((_num_consecutive_ancestors(x, y), y) 484 | for y in hfiles), 485 | reverse=True)) 486 | for x in cfiles] 487 | candidates.sort(reverse=True, 488 | key=lambda x: tuple(y for y, _ in x[1])) 489 | for cfile, hfile_candidates in candidates: 490 | for _, hfile in hfile_candidates: 491 | if hfile in hfiles: 492 | yield hfile.path, cfile.path 493 | hfiles.remove(hfile) 494 | break 495 | else: 496 | yield None, cfile.path 497 | 498 | for hfile in hfiles: 499 | yield hfile.path, None 500 | 501 | for filename, hfiles in hpaths.items(): 502 | if filename not in cpaths: 503 | self.components.extend( 504 | Component(x.path, None, self) for x in hfiles) 505 | else: 506 | cfiles = cpaths[filename] 507 | del cpaths[filename] 508 | self.components.extend( 509 | Component(x, y, self) for x, y in _pair(hfiles, cfiles)) 510 | 511 | for cfiles in cpaths.values(): 512 | self.components.extend( 513 | Component(None, x.path, self) for x in cfiles) 514 | 515 | def dependencies(self): 516 | """Returns dependency packages.""" 517 | if self.__dep_packages is None: 518 | self.__dep_packages = set() 519 | for component in self.components: 520 | self.__dep_packages.update(x.package 521 | for x in component.dependencies() 522 | if x.package != self) 523 | return self.__dep_packages 524 | 525 | 526 | class PackageGroup(object): 527 | """A collection of packages. 528 | 529 | Attributes: 530 | name: The unique name of the package group. 531 | path: The absolute path to the group directory. 532 | packages: {package_name: package} belonging to this group. 533 | """ 534 | 535 | def __init__(self, name, path): 536 | """Constructs an empty group. 537 | 538 | Args: 539 | name: A unique global identifier. 540 | path: The directory path to the group. 541 | 542 | Raises: 543 | InvalidArgumentError: The path is not a directory. 544 | """ 545 | if not os.path.isdir(path): 546 | raise InvalidArgumentError('%s is not a directory.' % path) 547 | self.name = name 548 | self.path = os.path.abspath(os.path.normpath(path)) 549 | self.packages = {} 550 | self.__dep_groups = None # set of dependency groups 551 | 552 | def __str__(self): 553 | """For printing graph nodes.""" 554 | return self.name 555 | 556 | def dependencies(self): 557 | """Returns dependency package groups.""" 558 | if self.__dep_groups is None: 559 | self.__dep_groups = set() 560 | for package in self.packages.values(): 561 | self.__dep_groups.update( 562 | x.group for x in package.dependencies() if x.group != self) 563 | return self.__dep_groups 564 | 565 | def add_package(self, package): 566 | """Adds a package into the group. 567 | 568 | This function is automatically called in the package constructor. 569 | 570 | Args: 571 | package: The constructed package. 572 | 573 | Raises: 574 | InvalidArgumentError: Duplicate package. 575 | """ 576 | if package.name in self.packages: 577 | raise InvalidArgumentError( 578 | '%s is a duplicate package in %s group.' % 579 | (package.name, self.name)) 580 | self.packages[package.name] = package 581 | 582 | 583 | class DependencyAnalysis(object): 584 | """Analysis of dependencies with package groups/packages/components. 585 | 586 | Attributes: 587 | config: The configuration dictionary. 588 | external_groups: External dependency packages and package groups. 589 | {group_name: PackageGroup} 590 | internal_groups: The package groups of the project under analysis. 591 | {group_name: PackageGroup} 592 | include_dirs: Directories to search for included headers. 593 | It is ordered, 594 | starting from internal and ending with external directories. 595 | """ 596 | 597 | def __init__(self, config_file): 598 | """Initializes analysis containers. 599 | 600 | Args: 601 | config_file: The path to the configuration file. 602 | 603 | Raises: 604 | YAMLError: Errors loading yaml files. 605 | SchemaError: The config file is malformed or invalid. 606 | InvalidArgumentError: The configuration has is invalid values. 607 | """ 608 | self.config = None 609 | self.external_groups = {} 610 | self.internal_groups = {} 611 | self.include_dirs = [] 612 | self._external_components = {} # {hpath: ExternalComponent} 613 | self._internal_components = {} # {hpath: Component} 614 | self.__package_aliases = [] # Sorted [(alias_path, external_package)] 615 | self.__include_patterns = [] # [(package, [regex])] 616 | self.__parse_config(config_file) 617 | self.__gather_include_dirs() 618 | self.__gather_aliases() 619 | self.__gather_include_patterns() 620 | self.make_components() 621 | 622 | def __parse_config(self, config_file_path): 623 | """Parses the configuration file. 624 | 625 | Args: 626 | config_file_path: The path to the configuration file. 627 | 628 | Raises: 629 | YAMLError: Errors loading yaml files. 630 | SchemaError: The configuration file is malformed or invalid. 631 | InvalidArgumentError: The configuration has invalid values. 632 | """ 633 | # Load before validation to check for well-formed YAML. 634 | with open(config_file_path) as config_file: 635 | self.config = safe_load(config_file) 636 | Validator(config_file_path, [_SCHEMA_FILE]).validate() 637 | 638 | for pkg_group_config in self.config['internal']: 639 | DependencyAnalysis.__add_package_group(pkg_group_config, 640 | self.internal_groups) 641 | for pkg_group_config in yaml_optional_list(self.config, 'external'): 642 | DependencyAnalysis.__add_package_group(pkg_group_config, 643 | self.external_groups) 644 | 645 | @staticmethod 646 | def __add_package_group(pkg_group_config, pkg_groups): 647 | """Initializes and adds a package group from configuration. 648 | 649 | Args: 650 | pkg_group_config: The package-group configuration dictionary. 651 | pkg_groups: The destination dictionary for member packages. 652 | 653 | Raises: 654 | InvalidArgumentError: Invalid configuration. 655 | """ 656 | group_name = pkg_group_config['name'] 657 | group_path = pkg_group_config['path'] 658 | if group_name in pkg_groups: 659 | raise InvalidArgumentError('Redefinition of %s group' % group_name) 660 | 661 | package_group = PackageGroup(group_name, group_path) 662 | 663 | for pkg_config in pkg_group_config['packages']: 664 | Package(pkg_config['name'], package_group, 665 | yaml_optional_list(pkg_config, 'src'), 666 | yaml_optional_list(pkg_config, 'include'), 667 | yaml_optional_list(pkg_config, 'alias'), 668 | yaml_optional_list(pkg_config, 'pattern'), 669 | yaml_optional_list(pkg_config, 'ignore')) 670 | 671 | pkg_groups[group_name] = package_group 672 | 673 | def __gather_include_dirs(self): 674 | """Gathers include directories from packages.""" 675 | 676 | def _add_from(groups): 677 | for group in groups.values(): 678 | for package in group.packages.values(): 679 | self.include_dirs.extend(package.include_paths) 680 | 681 | _add_from(self.internal_groups) 682 | _add_from(self.external_groups) 683 | 684 | def __gather_aliases(self): 685 | """Gathers aliases for *external* packages lazy include search.""" 686 | for group in self.external_groups.values(): 687 | for package in group.packages.values(): 688 | self.__package_aliases.extend( 689 | (x, package) for x in package.alias_paths) 690 | self.__package_aliases.sort() 691 | assert (len(set(x for x, _ in self.__package_aliases)) == len( 692 | self.__package_aliases)), "Ambiguous aliases to packages" 693 | 694 | def __gather_include_patterns(self): 695 | """Gathers and compiles include patterns into regex objects.""" 696 | for group in self.external_groups.values(): 697 | for package in group.packages.values(): 698 | self.__include_patterns.append( 699 | (package, [re.compile(x) for x in package.include_patterns 700 | ])) 701 | 702 | def locate(self, include, component): 703 | """Locates the dependency component. 704 | 705 | Args: 706 | include: The include object representing the directive. 707 | component: The dependent component. 708 | 709 | Returns: 710 | True if the include is found. 711 | 712 | Raises: 713 | AnalysisError: Failure to associate a header to a component. 714 | """ 715 | 716 | def _find_external_package(hpath): 717 | for path, package in reversed(self.__package_aliases): 718 | if path_isancestor(path, hpath): 719 | return package 720 | raise AnalysisError('include error: Cannot associate ' 721 | '%s file with any component.' % hpath) 722 | 723 | hpath, package = include.locate(component.working_dir, 724 | self.include_dirs, 725 | self.__include_patterns) 726 | 727 | if hpath is None: 728 | return False 729 | if package is None and hpath in self._internal_components: 730 | dep_component = self._internal_components[hpath] 731 | if dep_component != component: 732 | component.dep_components.add(dep_component) 733 | else: 734 | if hpath in self._external_components: 735 | component.dep_components.add(self._external_components[hpath]) 736 | else: 737 | dep_component = ExternalComponent( 738 | hpath, package or _find_external_package(hpath)) 739 | component.dep_components.add(dep_component) 740 | self._external_components[hpath] = dep_component 741 | return True 742 | 743 | @property 744 | def internal_components(self): 745 | """Yields components in internal groups.""" 746 | for group in self.internal_groups.values(): 747 | for package in group.packages.values(): 748 | for component in package.components: 749 | yield component 750 | 751 | def make_components(self): 752 | """Pairs hfiles and cfiles. 753 | 754 | Raises: 755 | AnalysisError: Misconfiguration or failure of the analysis. 756 | """ 757 | for group in self.internal_groups.values(): 758 | for package in group.packages.values(): 759 | package.construct_components() 760 | 761 | for component in self.internal_components: 762 | id_path = component.hpath or component.cpath 763 | self._internal_components[id_path] = component 764 | if component.cpath and component.cpath.endswith('.ipp'): 765 | self._internal_components[component.cpath] = component 766 | 767 | for component in self.internal_components: 768 | for include in itertools.chain(component.includes_in_h, 769 | component.includes_in_c): 770 | if not self.locate(include, component): 771 | warn('include issues: header not found: %s' % str(include)) 772 | 773 | def analyze(self, printer, args): 774 | """Runs the analysis.""" 775 | 776 | def _analyze(graph_name, digraph): 777 | digraph.analyze() 778 | digraph.print_cycles(printer) 779 | if not args.l and not args.L: 780 | digraph.print_levels(printer) 781 | else: 782 | digraph.print_levels(printer, args.l) 783 | digraph.print_summary(printer) 784 | digraph.write_dot(graph_name) 785 | 786 | if len(self.internal_groups) > 1: 787 | printer('\n' + '#' * 80) 788 | printer('analyzing dependencies among all package groups ...') 789 | _analyze( 790 | 'system', 791 | Graph(self.internal_groups.values(), 792 | iter, lambda x: x.name in self.external_groups)) 793 | 794 | for group_name, package_group in self.internal_groups.items(): 795 | if len(package_group.packages) > 1: 796 | printer('\n' + '#' * 80) 797 | printer('analyzing dependencies among packages in ' 798 | 'the specified package group %s ...' % group_name) 799 | 800 | def _dep_filter(nodes): 801 | return (node if node.group == package_group else node.group 802 | for node in nodes) 803 | 804 | _analyze( 805 | group_name, 806 | Graph(package_group.packages.values(), 807 | _dep_filter, lambda x: isinstance(x, PackageGroup))) 808 | 809 | for group_name, package_group in self.internal_groups.items(): 810 | for pkg_name, package in package_group.packages.items(): 811 | if not package.components: 812 | assert not package.src_paths 813 | continue 814 | printer('\n' + '#' * 80) 815 | printer('analyzing dependencies among components in ' 816 | 'the specified package %s.%s ...' % 817 | (group_name, pkg_name)) 818 | 819 | def _dep_filter(nodes): 820 | return (node if node.package == package else node.package 821 | for node in nodes) 822 | 823 | _analyze( 824 | '_'.join((group_name, pkg_name)), 825 | Graph(package.components, 826 | _dep_filter, lambda x: isinstance(x, Package))) 827 | -------------------------------------------------------------------------------- /cppdep/graph.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2016-2017 Olzhas Rakhimov 2 | # Copyright (C) 2010, 2014 Zhichang Yu 3 | # 4 | # This program is free software; you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation; either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with this program. If not, see . 16 | """Graph algorithms used in Large-Scale C++ Software Design (ch. 4, appendix C). 17 | 18 | A Python Graph API? http://wiki.python.org/moin/PythonGraphApi 19 | It seems that the best one is NetworkX(http://networkx.lanl.gov/). 20 | """ 21 | 22 | from __future__ import absolute_import, division 23 | 24 | import math 25 | 26 | import networkx as nx 27 | from networkx.drawing.nx_pydot import write_dot 28 | 29 | 30 | class Graph(object): 31 | """Graph for dependency analysis among its nodes. 32 | 33 | Attributes: 34 | digraph: The underlying directed graph without self-loops. 35 | """ 36 | 37 | def __init__(self, nodes, dep_filter=iter, is_external=lambda _: False): 38 | """Constructs a digraph for dependency analysis. 39 | 40 | Precondition: 41 | External nodes do not have successors in the graph. 42 | As a result, no cycles contain external nodes. 43 | All external nodes are at level 0. 44 | 45 | Args: 46 | nodes: Graph internal nodes with dependencies. 47 | dep_filter: A filter for node dependencies. 48 | is_external: Predicate to determine if a Graph node is external. 49 | """ 50 | self.digraph = nx.DiGraph() 51 | self.cycles = {} # {cyclic_graph: ([pre_edge], [suc_edge])} 52 | self.cycle2index = {} # {cyclic_graph: cycle_index} 53 | self.node2cycle = {} # {node: cyclic_graph} 54 | self.node2cd = {} # {node: cd} 55 | self.node2level = {} # {node: level} 56 | self.__dep_filter = dep_filter 57 | self.__is_external = is_external 58 | for node in nodes: 59 | assert not self.__is_external(node) 60 | self.digraph.add_node(node) 61 | for dependency in self.__dep_filter(node.dependencies()): 62 | assert node != dependency 63 | self.digraph.add_edge(node, dependency) 64 | 65 | # pylint: disable=invalid-name 66 | def __transitive_reduction(self): 67 | """Transitive reduction for acyclic graphs.""" 68 | assert nx.is_directed_acyclic_graph(self.digraph) 69 | for u in self.digraph: 70 | transitive_vertex = [] 71 | for v in self.digraph[u]: 72 | transitive_vertex.extend( 73 | x for _, x in nx.dfs_edges(self.digraph, v)) 74 | self.digraph.remove_edges_from((u, x) for x in transitive_vertex) 75 | 76 | def __condensation(self): 77 | """Produces condensation of cyclic graphs.""" 78 | subgraphs = nx.strongly_connected_component_subgraphs(self.digraph) 79 | for subgraph in list(subgraphs): 80 | if subgraph.number_of_nodes() == 1: 81 | continue # not a cycle 82 | pre_edges = [] 83 | suc_edges = [] 84 | for node in subgraph: 85 | assert node not in self.node2cycle 86 | assert node in self.digraph # no accidental copying 87 | self.node2cycle[node] = subgraph 88 | for pre_node in self.digraph.predecessors(node): 89 | if not subgraph.has_node(pre_node): 90 | pre_edges.append((pre_node, node)) 91 | self.digraph.add_edge(pre_node, subgraph) 92 | for suc_node in self.digraph.successors(node): 93 | if not subgraph.has_node(suc_node): 94 | suc_edges.append((node, suc_node)) 95 | self.digraph.add_edge(subgraph, suc_node) 96 | self.digraph.remove_node(node) 97 | assert subgraph not in self.cycles 98 | self.cycles[subgraph] = (pre_edges, suc_edges) 99 | 100 | cycle_order = lambda x: min(str(u) for u in x) 101 | for index, cycle in enumerate(sorted(self.cycles, key=cycle_order)): 102 | self.cycle2index[cycle] = index 103 | 104 | # pylint: disable=invalid-name 105 | def __decondensation(self): 106 | """Reverts the effect of the condensation.""" 107 | for subgraph, (pre_edges, suc_edges) in self.cycles.items(): 108 | assert self.digraph.has_node(subgraph) 109 | for u, v in pre_edges: 110 | if (self.digraph.has_edge(u, subgraph) or 111 | (u in self.node2cycle and 112 | self.digraph.has_edge(self.node2cycle[u], subgraph))): 113 | self.digraph.add_edge(u, v) 114 | for u, v in suc_edges: 115 | if (self.digraph.has_edge(subgraph, v) or 116 | (v in self.node2cycle and 117 | self.digraph.has_edge(subgraph, self.node2cycle[v]))): 118 | self.digraph.add_edge(u, v) 119 | self.digraph.add_nodes_from(subgraph) 120 | self.digraph.add_edges_from(subgraph.edges()) 121 | self.digraph.remove_node(subgraph) 122 | 123 | def analyze(self): 124 | """Applies transitive reduction to the graph and calculates metrics. 125 | 126 | If the graph contains cycles, 127 | the graph is minimized instead. 128 | """ 129 | assert self.digraph.number_of_selfloops() == 0 130 | self.__condensation() 131 | self.__transitive_reduction() 132 | self.__calculate_ccd() 133 | self.__calculate_levels() 134 | self.__decondensation() 135 | 136 | def __calculate_ccd(self): 137 | """Calculates CCD for nodes. 138 | 139 | The graph must be minimized with condensed cycles. 140 | """ 141 | descendants = {} # {node: set(descendant_node)} for memoization. 142 | 143 | def _get_descendants(node): 144 | """Returns a set of descendants of a node.""" 145 | if node not in descendants: 146 | node_descendants = set() 147 | for v in self.digraph[node]: 148 | node_descendants.add(v) 149 | node_descendants.update(_get_descendants(v)) 150 | descendants[node] = node_descendants 151 | return descendants[node] 152 | 153 | def _get_cd(node): 154 | """Returns CD contribution of a node.""" 155 | if self.__is_external(node): 156 | return 0 157 | return 1 if node not in self.cycles else node.number_of_nodes() 158 | 159 | for node in self.digraph: 160 | cd = _get_cd(node) 161 | for descendant in _get_descendants(node): 162 | cd += _get_cd(descendant) 163 | self.node2cd[node] = cd 164 | 165 | def __calculate_levels(self): 166 | """Calculates levels for nodes.""" 167 | 168 | def _get_level(node): 169 | if node not in self.node2level: 170 | level = (not self.__is_external(node) 171 | if node not in self.cycles else node.number_of_nodes()) 172 | if self.digraph[node]: 173 | level += max(_get_level(x) for x in self.digraph[node]) 174 | self.node2level[node] = level 175 | return self.node2level[node] 176 | 177 | for node in self.digraph: 178 | _get_level(node) 179 | 180 | def get_level(self, node): 181 | """Returns the level of the component node.""" 182 | if node in self.node2cycle: 183 | return self.node2level[self.node2cycle[node]] 184 | return self.node2level[node] 185 | 186 | def print_cycles(self, printer): 187 | """Prints cycles only after reduction.""" 188 | if not self.cycles: 189 | return 190 | printer('=' * 80) 191 | printer('%d cycles detected:\n' % len(self.cycles)) 192 | for cycle, i in sorted(self.cycle2index.items(), key=lambda x: x[1]): 193 | printer('cycle #%d (%d nodes):' % (i, cycle.number_of_nodes()), 194 | ', '.join(sorted(str(x) for x in cycle.nodes()))) 195 | printer( 196 | 'cycle #%d (%d edges):' % (i, cycle.number_of_edges()), 197 | ' '.join( 198 | sorted( 199 | str(edge[0]) + '->' + str(edge[1]) 200 | for edge in cycle.edges()))) 201 | printer() 202 | 203 | def print_levels(self, printer, reduced_dependencies=None): 204 | """Prints levels of nodes. 205 | 206 | Args: 207 | printer: The printer object. 208 | reduced_dependencies: Print node dependencies in reduced form. 209 | If None, no dependencies are printed at all. 210 | """ 211 | printer('=' * 80) 212 | max_level = max(self.node2level.values()) 213 | printer('%d level(s):\n' % max_level) 214 | 215 | def _stabilize(node): 216 | """Returns string for report stabilization sort.""" 217 | if node in self.cycles: 218 | return min(str(x) for x in node) 219 | return str(node) 220 | 221 | def _print_dependencies(node): 222 | """Prints dependencies of the levelized components.""" 223 | if reduced_dependencies is None or self.__is_external(node): 224 | return 225 | for v in sorted(self.digraph[node] if reduced_dependencies else set( 226 | self.__dep_filter(node.dependencies())), 227 | key=lambda x: (self.get_level(x), str(x))): 228 | if v in self.node2cycle: 229 | cycle = self.node2cycle[v] 230 | printer('\t\t%d. %s <%d>' % (self.node2level[cycle], str(v), 231 | self.cycle2index[cycle])) 232 | else: 233 | printer('\t\t%d. %s' % (self.node2level[v], str(v))) 234 | 235 | level_num = -1 236 | for node, level in sorted(self.node2level.items(), 237 | key=lambda x: (x[1], _stabilize(x[0]))): 238 | while level > level_num: 239 | level_num += 1 240 | printer('level %d:' % level_num) 241 | if node in self.cycles: 242 | cycle_index = self.cycle2index[node] 243 | for v in sorted(node, key=str): 244 | printer('\t%s <%d>' % (str(v), cycle_index)) 245 | _print_dependencies(v) 246 | else: 247 | printer('\t' + str(node)) 248 | _print_dependencies(node) 249 | 250 | def print_summary(self, printer): 251 | """Calculates and prints overall CCD metrics.""" 252 | ccd = 0 253 | for node, cd in self.node2cd.items(): 254 | if node in self.cycles: 255 | ccd += node.number_of_nodes() * cd 256 | else: 257 | ccd += cd 258 | num_nodes = len([x for x in self.digraph if not self.__is_external(x)]) 259 | average_cd = ccd / num_nodes 260 | # CCD_Balanced_BTree = (N + 1) * log2(N + 1) - N 261 | ccd_btree = (num_nodes + 1) * math.log(num_nodes + 1, 2) - num_nodes 262 | normalized_ccd = ccd / ccd_btree 263 | printer('=' * 80) 264 | printer('SUMMARY:') 265 | printer('Components: %d\t Cycles: %d\t Levels: %d' % 266 | (num_nodes, len(self.cycles), max(self.node2level.values()))) 267 | typical_range = '[0.85, 1.10]' 268 | printer('CCD: %d\t ACCD: %.2f\t NCCD: %.2f (typical range is %s)' % 269 | (ccd, average_cd, normalized_ccd, typical_range)) 270 | 271 | def write_dot(self, file_basename): 272 | """Writes graph into a file in Graphviz DOT format. 273 | 274 | Args: 275 | file_basename: The output file name without extension. 276 | """ 277 | write_dot(self.digraph, file_basename + '.dot') 278 | -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rakhimov/cppdep/307ba5838a2b5a44c661b41b9be7a8e91b002849/logo.png -------------------------------------------------------------------------------- /requirements-dev.txt: -------------------------------------------------------------------------------- 1 | coverage 2 | pytest-cov 3 | prospector 4 | yapf 5 | -------------------------------------------------------------------------------- /requirements-test.txt: -------------------------------------------------------------------------------- 1 | mock 2 | pytest 3 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | networkx 2 | pydot 3 | pydotplus 4 | PyYAML 5 | PyKwalify>=1.6.0 6 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """The setup script to generate dist files for PyPi. 3 | 4 | To upload the release to PyPi: 5 | $ ./setup.py sdist bdist_wheel --universal 6 | $ twine upload dist/* 7 | """ 8 | 9 | from setuptools import setup 10 | 11 | from cppdep import cppdep 12 | 13 | setup( 14 | name="cppdep", 15 | version=cppdep.VERSION, 16 | maintainer="Olzhas Rakhimov", 17 | maintainer_email="ol.rakhimov@gmail.com", 18 | description="Dependency analyzer for C/C++ projects", 19 | download_url="https://github.com/rakhimov/cppdep", 20 | license="GPLv3+", 21 | install_requires=[ 22 | "networkx", 23 | "pydot", 24 | "pydotplus", 25 | "PyYAML", 26 | "PyKwalify>=1.6.0" 27 | ], 28 | keywords=["c++", "c", "static analysis", "dependency analysis"], 29 | url="http://github.com/rakhimov/cppdep", 30 | packages=["cppdep"], 31 | package_data={"cppdep": ["config_schema.yml"]}, 32 | entry_points={"console_scripts": ["cppdep = cppdep.__main__:main"]}, 33 | long_description=open("README.rst").read(), 34 | classifiers=[ 35 | "Development Status :: 4 - Beta", 36 | "Intended Audience :: Developers", 37 | "Topic :: Software Development :: Quality Assurance", 38 | "License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)", 39 | "Programming Language :: C", 40 | "Programming Language :: C++", 41 | "Operating System :: POSIX", 42 | "Operating System :: Microsoft :: Windows", 43 | "Operating System :: MacOS :: MacOS X", 44 | "Environment :: Console", 45 | "Programming Language :: Python :: 2.7", 46 | "Programming Language :: Python :: 3.4", 47 | "Programming Language :: Python :: 3.5" 48 | ], 49 | ) 50 | -------------------------------------------------------------------------------- /test/test_cppdep.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2017 Olzhas Rakhimov 2 | # 3 | # This program is free software; you can redistribute it and/or modify 4 | # it under the terms of the GNU General Public License as published by 5 | # the Free Software Foundation; either version 3 of the License, or 6 | # (at your option) any later version. 7 | # 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU General Public License for more details. 12 | # 13 | # You should have received a copy of the GNU General Public License 14 | # along with this program. If not, see . 15 | 16 | """Tests for the analysis facilities.""" 17 | 18 | from __future__ import absolute_import 19 | 20 | import os 21 | import platform 22 | import re 23 | 24 | import mock 25 | import pytest 26 | 27 | from cppdep import cppdep 28 | from cppdep.cppdep import Include 29 | 30 | 31 | def path_relpath_posix(path, root): 32 | """Returns relative path with posix separators.""" 33 | return cppdep.path_to_posix_sep(os.path.relpath(path, root)) 34 | 35 | 36 | @pytest.mark.parametrize('filename,expected', 37 | [('path.cc', 'path'), ('path.', 'path'), 38 | ('.path', '.path'), ('path', 'path'), 39 | ('very/long/path.h', 'very/long/path'), 40 | ('./.././path.cc', './.././path')]) 41 | def test_strip_ext(filename, expected): 42 | """Test extraction of file name.""" 43 | assert cppdep.strip_ext(filename) == expected 44 | 45 | 46 | @pytest.mark.skipif(platform.system() == 'Windows', reason='non-POSIX') 47 | @pytest.mark.parametrize('path,paths,expected', 48 | [('root', ('../file',), 'file'), 49 | ('root', ('file',), 'root/file'), 50 | ('.', ('./file',), 'file')]) 51 | def test_path_normjoin_posix(path, paths, expected): 52 | """Test the normalized join of paths on POSIX systems.""" 53 | assert cppdep.path_normjoin(path, *paths) == expected 54 | 55 | 56 | @pytest.mark.skipif(platform.system() != 'Windows', reason='non-DOS') 57 | @pytest.mark.parametrize('path,paths,expected', 58 | [(r'C:\root', (r'..\file',), r'C:\file'), 59 | ('root', ('file',), r'root\file'), 60 | ('.', (r'.\file',), 'file'), 61 | ('root\\', ('dir/file',), r'root\dir\file')]) 62 | def test_path_normjoin_dos(path, paths, expected): 63 | """Test the normalized join of paths on DOS systems.""" 64 | assert cppdep.path_normjoin(path, *paths) == expected 65 | 66 | 67 | @pytest.mark.skipif(platform.system() == 'Windows', 68 | reason='The same logic with different path separators.') 69 | @pytest.mark.parametrize('paths,expected', 70 | [(['/path', '/path/file', '/path/file2'], '/path'), 71 | (['/path', '/dir'], '/'), 72 | (['/path/file', '/pa'], '/'), 73 | (['/path/dir/file', '/path/dir1/file'], '/path'), 74 | (['/path/dir/', '/path/dir/file'], '/path/dir'), 75 | ([], ''), (['/dir'], '/dir'), 76 | pytest.mark.xfail((['/dir/*'], '/dir'))]) 77 | def test_path_common_posix(paths, expected): 78 | """Test common directory for paths.""" 79 | assert cppdep.path_common(paths) == expected 80 | 81 | 82 | @pytest.mark.skipif(platform.system() == 'Windows', 83 | reason='The same logic with different path separators.') 84 | @pytest.mark.parametrize('parent,child,expected', 85 | [('/dir', '/dir/file', True), 86 | ('/dir/file', '/dir', False), 87 | ('/dir/', '/dir/file', True), 88 | ('/di', '/dir/file', False), 89 | ('/', '/dir/file', True), 90 | ('/dir', '/dir/dir2/file', True), 91 | ('/tar', '/dir/file', False), 92 | ('/dir', '/dir', True)]) 93 | def test_path_isancestor(parent, child, expected): 94 | """Test proper ancestor directory check for paths.""" 95 | assert cppdep.path_isancestor(parent, child) == expected 96 | 97 | 98 | @pytest.mark.skipif(platform.system() != 'Windows', reason='POSIX is noop.') 99 | @pytest.mark.parametrize('path,expected', 100 | [('file', 'file'), ('/dir/file', '/dir/file'), 101 | (r'\dir\file', '/dir/file'), 102 | (r'/dir\file', '/dir/file')]) 103 | def test_path_to_posix_sep(path, expected): 104 | """Test POSIX separator normalization for DOS paths.""" 105 | assert cppdep.path_to_posix_sep(path) == expected 106 | 107 | 108 | @pytest.mark.parametrize('dictionary,element,default_value,expected', 109 | [({'tag': 'value'}, 'tag', 'default', 'value'), 110 | ({}, 'tag', 'default', 'default'), 111 | ({'label': 'value'}, 'tag', 'default', 'default')]) 112 | def test_yaml_optional(dictionary, element, default_value, expected): 113 | """Test retrieval of an optional value from yaml configuration.""" 114 | assert cppdep.yaml_optional(dictionary, element, default_value) == expected 115 | 116 | 117 | @pytest.mark.parametrize( 118 | 'dictionary,element,expected', 119 | [pytest.mark.xfail(({'tag': 'value'}, 'tag', ['value'])), 120 | ({'tag': ['value']}, 'tag', ['value']), 121 | ({}, 'tag', []), 122 | ({'label': 'value'}, 'tag', [])]) 123 | def test_yaml_optional_list(dictionary, element, expected): 124 | """Test special handling of optional lists in yaml configurations.""" 125 | assert (cppdep.yaml_optional_list(dictionary, element) == expected) 126 | 127 | 128 | @pytest.mark.parametrize( 129 | 'include,expected', 130 | [(Include('vector', with_quotes=True), '"vector"'), 131 | (Include('vector', with_quotes=False), ''), 132 | (Include('dir/vector.h', with_quotes=False), ''), 133 | (Include(r'dir\vector.h', with_quotes=False), r'')]) 134 | def test_include_str(include, expected): 135 | """Tests proper string representation of include upon string conversion.""" 136 | assert str(include) == expected 137 | 138 | 139 | @pytest.mark.parametrize( 140 | 'include_one,include_two', 141 | [(Include('vector', True), Include('vector', True)), 142 | (Include('vector', True), Include('vector', False)), 143 | (Include('./vector', True), Include('vector', True)), 144 | (Include('include/./vector', True), Include('include/vector', True))]) 145 | def test_include_eq(include_one, include_two): 146 | """Include equality and hash tests for storage in containers.""" 147 | assert include_one == include_two 148 | assert hash(include_one) == hash(include_two) 149 | 150 | 151 | def test_include_ne_impl(): 152 | """Makes sure that __ne__ is implemented.""" 153 | with mock.patch('cppdep.cppdep.Include.__eq__') as mock_eq: 154 | include_one = Include('vector', True) 155 | check = include_one != include_one 156 | assert mock_eq.called 157 | assert not check 158 | 159 | 160 | @pytest.mark.parametrize( 161 | 'include_one,include_two', 162 | [(Include('vector.hpp', True), Include('vector', True)), 163 | (Include('dir/vector', True), Include('include/vector', True))]) 164 | def test_include_neq(include_one, include_two): 165 | """__ne__ doesn't imply (not __eq__) in Python.""" 166 | assert include_one != include_two 167 | 168 | 169 | @pytest.mark.parametrize( 170 | 'text,expected', 171 | [('#include ', ['']), 172 | ('#include "vector"', ['"vector"']), 173 | ('# include ', ['']), 174 | ('#\tinclude ', ['']), 175 | ('#include "vector.h"', ['"vector.h"']), 176 | ('#include "vector.h++"', ['"vector.h++"']), 177 | ('#include "vector.any"', ['"vector.any"']), 178 | ('#include "vector.hpp"', ['"vector.hpp"']), 179 | ('#include "vector.cpp"', ['"vector.cpp"']), 180 | ('#include "dir/vector.hpp"', ['"dir/vector.hpp"']), 181 | (r'#include "dir\vector.hpp"', [r'"dir\vector.hpp"']), 182 | ('#include "./vector"', ['"./vector"']), 183 | ('#include <./vector>', ['<./vector>']), 184 | ('#include \n#include ', ['', '']), 185 | ('#include \n#include ', ['', '']), 186 | ('#include // a>', ['']), 187 | ('#include "b" // a"', ['"b"']), 188 | ('#include /* a> */', ['']), 189 | ('#include "b" /* a" */', ['"b"']), 190 | ('#include ""', []), 191 | ('#include <>', []), 192 | ('//#include ', []), 193 | ('/*#include */', []), 194 | ('#import ', []), 195 | ('include ', []), 196 | ('#nclude ', []), 197 | ('', []), 198 | ('"vector"', []), 199 | ('#', []), 200 | ('#include < vector>', []), 201 | ('#include ', []), 202 | ('#include ', []), 203 | ('#include " vector"', []), 204 | ('#include "vector "', []), 205 | (' #include ', ['']), 206 | ('#include ', ['']), 207 | ('some_code #include ', []), 208 | ('#include some_code', ['']), 209 | pytest.mark.xfail(('#if 0\n#include \n#endif', [])), 210 | pytest.mark.xfail(('/*\n#include \n*/', [])), 211 | pytest.mark.xfail(('#define V \n#include V\n', ['']))]) 212 | def test_include_grep(text, expected, tmpdir): 213 | """Tests the include directive search from a text.""" 214 | src = tmpdir.join('include_grep') 215 | src.write(text) 216 | assert [str(x) for x in Include.grep(str(src))] == expected 217 | 218 | 219 | @pytest.fixture() 220 | def include_setup(tmpdir): 221 | """Sets up the system for include header search.""" 222 | dirs = ['project1', 'external1', 'external2'] 223 | files = [tmpdir.mkdir(x).join('header').write('') for x in dirs] 224 | return tmpdir, [os.path.join(str(tmpdir), x) for x in dirs] 225 | 226 | 227 | #pylint: disable=redefined-outer-name 228 | @pytest.mark.parametrize( 229 | 'include,cwd,include_dirs,expected', 230 | [(Include('header', True), '.', [], None), 231 | (Include('header', True), 'project1', [], 'project1/header'), 232 | (Include('header', False), 'project1', [], None), 233 | (Include('header', True), 'project1', ['external2', 'external1'], 234 | 'project1/header'), 235 | (Include('header', False), 'project1', ['external2', 'external1'], 236 | 'external1/header'), 237 | (Include('header', False), 'project1', ['external1', 'external2'], 238 | 'external2/header')]) 239 | def test_include_locate(include, cwd, include_dirs, expected, include_setup): 240 | """The search for header locations from include paths.""" 241 | tmpdir, _ = include_setup 242 | abs_cwd = cppdep.path_normjoin(str(tmpdir), cwd) 243 | include_dirs = [cppdep.path_normjoin(str(tmpdir), x) for x in include_dirs] 244 | hpath, package = include.locate(abs_cwd, include_dirs, []) 245 | assert package is None 246 | assert include.hpath == hpath 247 | if expected is None: 248 | assert include.hpath is None 249 | else: 250 | assert include.hpath is not None 251 | assert path_relpath_posix(include.hpath, str(tmpdir)) == expected 252 | 253 | 254 | @pytest.mark.parametrize( 255 | 'include,cwd,include_patterns,expected', 256 | [(Include('header_foo', True), '.', [], (None, None)), 257 | (Include('header', True), '.', [('foo', 'header')], ('header', 'foo')), 258 | (Include('header', False), '.', [('foo', 'header')], ('header', 'foo')), 259 | (Include('header', False), 'project1', [('foo', 'header')], 260 | ('header', 'foo')), 261 | (Include('header', False), '.', [('foo', 'header'), ('bar', 'header')], 262 | ('header', 'foo')), 263 | (Include('header', False), '.', [('bar', 'header'), ('foo', 'header')], 264 | ('header', 'bar')), 265 | (Include('header_foo', False), '.', [('foo', 'header$')], (None, None)), 266 | (Include('header_foo', False), '.', [('foo', 'header')], 267 | ('header_foo', 'foo')), 268 | (Include('header_foo', False), '.', [('foo', 'header_foo')], 269 | ('header_foo', 'foo'))]) 270 | def test_include_locate_pattern(include, cwd, include_patterns, expected, 271 | include_setup): 272 | """Pattern based include header location.""" 273 | tmpdir, include_dirs = include_setup 274 | abs_cwd = cppdep.path_normjoin(str(tmpdir), cwd) 275 | include_patterns = [(x, [re.compile(y)]) for x, y in include_patterns] 276 | assert include.locate(abs_cwd, include_dirs, include_patterns) == expected 277 | 278 | 279 | @pytest.mark.parametrize('hpath,cpath', 280 | [('header', None), (None, 'source'), 281 | ('header', 'source')]) 282 | def test_component_init(hpath, cpath, tmpdir, monkeypatch): 283 | """Component construction from header and implementation files.""" 284 | mock_warn = mock.MagicMock(spec=cppdep.warn) 285 | monkeypatch.setattr(cppdep, 'warn', mock_warn) 286 | package = mock.MagicMock(spec=cppdep.Package) 287 | package.name = 'mock_package' 288 | package.group = mock.MagicMock(spec=cppdep.PackageGroup) 289 | package.group.name = 'mock_group' 290 | package.root = str(tmpdir) 291 | if hpath: 292 | tmpdir.join(hpath).write('') 293 | hpath = cppdep.path_normjoin(str(tmpdir), hpath) 294 | if cpath: 295 | tmpdir.join(cpath).write('') 296 | cpath = cppdep.path_normjoin(str(tmpdir), cpath) 297 | component = cppdep.Component(hpath, cpath, package) 298 | assert (component.name == 299 | path_relpath_posix(cppdep.strip_ext(cpath or hpath), str(tmpdir))) 300 | assert str(component) == component.name 301 | assert component.package == package 302 | assert component.hpath == hpath 303 | assert component.cpath == cpath 304 | assert hpath or mock_warn.called 305 | 306 | 307 | @pytest.mark.parametrize('filename,is_header', 308 | [('', None), ('.file', None), ('header', True), 309 | ('head.er', None), ('header.h', True), 310 | ('head.er.h', None), ('dir/header.h', None), 311 | ('header.hpp', True), ('header.h++', True), 312 | ('header.hh', True), ('header.hxx', True), 313 | ('src.c', False), ('src.cc', False), 314 | ('src.c++', False), ('src.cxx', False), 315 | ('src.cpp', False), ('src.java', None), 316 | ('unconvetional header.hpp', None)]) 317 | def test_package_src_regex(filename, is_header): 318 | """Test the regex for matching and gathering C/C++ header/source files.""" 319 | src_match = cppdep.Package._RE_SRC.match(filename) 320 | if is_header is None: 321 | assert not src_match 322 | elif is_header: 323 | assert src_match.group('h') is not None 324 | else: 325 | assert src_match.group('c') is not None 326 | -------------------------------------------------------------------------------- /test/test_graph.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2016-2017 Olzhas Rakhimov 2 | # Copyright (C) 2010, 2014 Zhichang Yu 3 | # 4 | # This program is free software; you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation; either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with this program. If not, see . 16 | 17 | """Tests for graph extension functions.""" 18 | 19 | from __future__ import print_function, absolute_import 20 | 21 | import pytest 22 | 23 | from cppdep import graph 24 | 25 | #pylint: disable=redefined-outer-name 26 | 27 | @pytest.fixture() 28 | def small_graph(): 29 | """A small dependency graph with multiple cycles.""" 30 | dependency_graph = graph.Graph([]) 31 | digraph = dependency_graph.digraph 32 | edges1 = [(1, 2), (2, 4), (2, 6), (6, 2), (6, 7), (7, 6)] 33 | edges2 = [(1, 3), (1, 5), (3, 4), (3, 5), (3, 8), (8, 9), (9, 3)] 34 | edges3 = [(10, 11), (10, 12), (11, 12), (12, 11)] 35 | digraph.add_edges_from(edges1) 36 | digraph.add_edges_from(edges2) 37 | digraph.add_edges_from(edges3) 38 | return dependency_graph 39 | 40 | 41 | def test_graph_init(small_graph): 42 | """Test the graph creation.""" 43 | digraph = small_graph.digraph 44 | assert set(digraph) == set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) 45 | assert set(digraph.edges()) == set([(1, 2), (1, 3), (1, 5), (10, 11), 46 | (10, 12), (11, 12), (12, 11), (2, 4), 47 | (2, 6), (3, 4), (3, 5), (3, 8), (6, 2), 48 | (6, 7), (7, 6), (8, 9), (9, 3)]) 49 | 50 | 51 | @pytest.fixture() 52 | def dep_graph(small_graph): 53 | """Sets up the analyzed graph.""" 54 | small_graph.analyze() 55 | return small_graph 56 | 57 | 58 | def test_graph_minimal(dep_graph): 59 | """Test the graph after minimization.""" 60 | digraph = dep_graph.digraph 61 | assert set(digraph) == set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) 62 | assert set(digraph.edges()) == set([(1, 2), (1, 3), (10, 11), 63 | (10, 12), (11, 12), (12, 11), (2, 4), 64 | (2, 6), (3, 4), (3, 5), (3, 8), (6, 2), 65 | (6, 7), (7, 6), (8, 9), (9, 3)]) 66 | 67 | 68 | def test_graph_cycles(dep_graph): 69 | """Test graph cycles after minimization/analysis.""" 70 | expected_cycles = {(2, 6, 7): set([(2, 6), (6, 2), (6, 7), (7, 6)]), 71 | (3, 8, 9): set([(3, 8), (8, 9), (9, 3)]), 72 | (11, 12): set([(11, 12), (12, 11)])} 73 | graph_cycles = {tuple(sorted(cycle)): set(cycle.edges()) 74 | for cycle in dep_graph.cycles} 75 | assert graph_cycles == expected_cycles 76 | 77 | 78 | def test_print_cycles(dep_graph, capsys): 79 | """Test report of cycles.""" 80 | dep_graph.print_cycles(print) 81 | out, _ = capsys.readouterr() 82 | assert out.split('\n') == ['=' * 80, '3 cycles detected:', '', 83 | 'cycle #0 (2 nodes): 11, 12', 84 | 'cycle #0 (2 edges): 11->12 12->11', '', 85 | 'cycle #1 (3 nodes): 2, 6, 7', 86 | 'cycle #1 (4 edges): 2->6 6->2 6->7 7->6', '', 87 | 'cycle #2 (3 nodes): 3, 8, 9', 88 | 'cycle #2 (3 edges): 3->8 8->9 9->3', '', ''] 89 | 90 | 91 | def test_print_levels(dep_graph, capsys): 92 | """Test the reporting of node levels.""" 93 | dep_graph.print_levels(print) 94 | out, _ = capsys.readouterr() 95 | assert out.split('\n') == ['=' * 80, '5 level(s):', '', 'level 0:', 96 | 'level 1:', '\t4', '\t5', 97 | 'level 2:', '\t11 <0>', '\t12 <0>', 98 | 'level 3:', '\t10', 99 | 'level 4:', '\t2 <1>', '\t6 <1>', '\t7 <1>', 100 | '\t3 <2>', '\t8 <2>', '\t9 <2>', 101 | 'level 5:', '\t1', ''] 102 | 103 | 104 | def test_print_levels_with_deps(dep_graph, capsys): 105 | """Test the reporting of node levels with reduced dependencies.""" 106 | dep_graph.print_levels(print, reduced_dependencies=True) 107 | out, _ = capsys.readouterr() 108 | assert out.split('\n') == ['=' * 80, '5 level(s):', '', 'level 0:', 109 | 'level 1:', '\t4', '\t5', 110 | 'level 2:', 111 | '\t11 <0>', '\t\t2. 12 <0>', 112 | '\t12 <0>', '\t\t2. 11 <0>', 113 | 'level 3:', 114 | '\t10', '\t\t2. 11 <0>', '\t\t2. 12 <0>', 115 | 'level 4:', 116 | '\t2 <1>', '\t\t1. 4', '\t\t4. 6 <1>', 117 | '\t6 <1>', '\t\t4. 2 <1>', '\t\t4. 7 <1>', 118 | '\t7 <1>', '\t\t4. 6 <1>', 119 | '\t3 <2>', '\t\t1. 4', '\t\t1. 5', 120 | '\t\t4. 8 <2>', 121 | '\t8 <2>', '\t\t4. 9 <2>', 122 | '\t9 <2>', '\t\t4. 3 <2>', 123 | 'level 5:', 124 | '\t1', '\t\t4. 2 <1>', '\t\t4. 3 <2>', ''] 125 | 126 | 127 | def test_print_summary(dep_graph, capsys): 128 | """Test the summary report.""" 129 | dep_graph.print_summary(print) 130 | out, _ = capsys.readouterr() 131 | assert out.split('\n') == ['=' * 80, 'SUMMARY:', 132 | 'Components: 12\t Cycles: 3\t Levels: 5', 133 | 'CCD: 45\t ACCD: 3.75\t NCCD: 1.25 ' 134 | '(typical range is [0.85, 1.10])', ''] 135 | --------------------------------------------------------------------------------