~prokop/lua-pebble

015fbcd51fab9e3148435a428cf7bfd1e926a1f0 — Prokop Randacek 10 months ago 1db5b63 master
upkeep
D LICENSE => LICENSE +0 -19
@@ 1,19 0,0 @@
The MIT License (MIT) Copyright © 2022 Prokop Randáček

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the “Software”), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

A LICENSES/CC0-1.0.txt => LICENSES/CC0-1.0.txt +121 -0
@@ 0,0 1,121 @@
Creative Commons Legal Code

CC0 1.0 Universal

    CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
    LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
    ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
    INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
    REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
    PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
    THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
    HEREUNDER.

Statement of Purpose

The laws of most jurisdictions throughout the world automatically confer
exclusive Copyright and Related Rights (defined below) upon the creator
and subsequent owner(s) (each and all, an "owner") of an original work of
authorship and/or a database (each, a "Work").

Certain owners wish to permanently relinquish those rights to a Work for
the purpose of contributing to a commons of creative, cultural and
scientific works ("Commons") that the public can reliably and without fear
of later claims of infringement build upon, modify, incorporate in other
works, reuse and redistribute as freely as possible in any form whatsoever
and for any purposes, including without limitation commercial purposes.
These owners may contribute to the Commons to promote the ideal of a free
culture and the further production of creative, cultural and scientific
works, or to gain reputation or greater distribution for their Work in
part through the use and efforts of others.

For these and/or other purposes and motivations, and without any
expectation of additional consideration or compensation, the person
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
is an owner of Copyright and Related Rights in the Work, voluntarily
elects to apply CC0 to the Work and publicly distribute the Work under its
terms, with knowledge of his or her Copyright and Related Rights in the
Work and the meaning and intended legal effect of CC0 on those rights.

1. Copyright and Related Rights. A Work made available under CC0 may be
protected by copyright and related or neighboring rights ("Copyright and
Related Rights"). Copyright and Related Rights include, but are not
limited to, the following:

  i. the right to reproduce, adapt, distribute, perform, display,
     communicate, and translate a Work;
 ii. moral rights retained by the original author(s) and/or performer(s);
iii. publicity and privacy rights pertaining to a person's image or
     likeness depicted in a Work;
 iv. rights protecting against unfair competition in regards to a Work,
     subject to the limitations in paragraph 4(a), below;
  v. rights protecting the extraction, dissemination, use and reuse of data
     in a Work;
 vi. database rights (such as those arising under Directive 96/9/EC of the
     European Parliament and of the Council of 11 March 1996 on the legal
     protection of databases, and under any national implementation
     thereof, including any amended or successor version of such
     directive); and
vii. other similar, equivalent or corresponding rights throughout the
     world based on applicable law or treaty, and any national
     implementations thereof.

2. Waiver. To the greatest extent permitted by, but not in contravention
of, applicable law, Affirmer hereby overtly, fully, permanently,
irrevocably and unconditionally waives, abandons, and surrenders all of
Affirmer's Copyright and Related Rights and associated claims and causes
of action, whether now known or unknown (including existing as well as
future claims and causes of action), in the Work (i) in all territories
worldwide, (ii) for the maximum duration provided by applicable law or
treaty (including future time extensions), (iii) in any current or future
medium and for any number of copies, and (iv) for any purpose whatsoever,
including without limitation commercial, advertising or promotional
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
member of the public at large and to the detriment of Affirmer's heirs and
successors, fully intending that such Waiver shall not be subject to
revocation, rescission, cancellation, termination, or any other legal or
equitable action to disrupt the quiet enjoyment of the Work by the public
as contemplated by Affirmer's express Statement of Purpose.

3. Public License Fallback. Should any part of the Waiver for any reason
be judged legally invalid or ineffective under applicable law, then the
Waiver shall be preserved to the maximum extent permitted taking into
account Affirmer's express Statement of Purpose. In addition, to the
extent the Waiver is so judged Affirmer hereby grants to each affected
person a royalty-free, non transferable, non sublicensable, non exclusive,
irrevocable and unconditional license to exercise Affirmer's Copyright and
Related Rights in the Work (i) in all territories worldwide, (ii) for the
maximum duration provided by applicable law or treaty (including future
time extensions), (iii) in any current or future medium and for any number
of copies, and (iv) for any purpose whatsoever, including without
limitation commercial, advertising or promotional purposes (the
"License"). The License shall be deemed effective as of the date CC0 was
applied by Affirmer to the Work. Should any part of the License for any
reason be judged legally invalid or ineffective under applicable law, such
partial invalidity or ineffectiveness shall not invalidate the remainder
of the License, and in such case Affirmer hereby affirms that he or she
will not (i) exercise any of his or her remaining Copyright and Related
Rights in the Work or (ii) assert any associated claims and causes of
action with respect to the Work, in either case contrary to Affirmer's
express Statement of Purpose.

4. Limitations and Disclaimers.

 a. No trademark or patent rights held by Affirmer are waived, abandoned,
    surrendered, licensed or otherwise affected by this document.
 b. Affirmer offers the Work as-is and makes no representations or
    warranties of any kind concerning the Work, express, implied,
    statutory or otherwise, including without limitation warranties of
    title, merchantability, fitness for a particular purpose, non
    infringement, or the absence of latent or other defects, accuracy, or
    the present or absence of errors, whether or not discoverable, all to
    the greatest extent permissible under applicable law.
 c. Affirmer disclaims responsibility for clearing rights of other persons
    that may apply to the Work or any use thereof, including without
    limitation any person's Copyright and Related Rights in the Work.
    Further, Affirmer disclaims responsibility for obtaining any necessary
    consents, permissions or other rights required for any use of the
    Work.
 d. Affirmer understands and acknowledges that Creative Commons is not a
    party to this document and has no duty or obligation with respect to
    this CC0 or use of the Work.

A LICENSES/MIT.txt => LICENSES/MIT.txt +9 -0
@@ 0,0 1,9 @@
MIT License

Copyright (c) <year> <copyright holders>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

M README.md => README.md +24 -1
@@ 1,10 1,14 @@
<!--
SPDX-FileCopyrightText: 2023 Prokop Randáček <prokop@rdck.dev>
SPDX-License-Identifier: CC0-1.0
-->
# Pebble

Lua pebble can fetch and cache dependencies from the internet on demand.

---

Compatible with lua 5.4 and luajit.
Compatible with lua 5.4 or luajit on Linux.

## Usage



@@ 34,3 38,22 @@ install pebble.lua /usr/share/lua/common/
rm /usr/share/lua/common/pebble.lua
```

## Details

Pebble hooks into the `require` function. When it detects you trying to require
some library using a pebble string, it checks to see if it is found inside
`~/.cache/luapebble`. If it is there or if it is too old, it refetches the
dependency. Then it loads the library and returns it.

## Config

You can define a global `PEBBLE` table to alter pebble behaviour. The default
values are:

```lua
PEBBLE = {
    max_age_seconds = 60*60*24*7, -- Older than this gets refetched (week)
    quiet = false, -- Dont print to stdout
    verbose = false, -- Echo commands before executing
}
```

M example_github.lua => example_github.lua +2 -1
@@ 1,4 1,5 @@
-- SPDX-License-Identifier: MIT
-- SPDX-FileCopyrightText: 2023 Prokop Randáček <prokop@rdck.dev>
-- SPDX-License-Identifier: CC0-1.0
require('pebble') -- init pebble

-- load a file from github using its pebble string

M example_github_tag.lua => example_github_tag.lua +2 -1
@@ 1,4 1,5 @@
-- SPDX-License-Identifier: MIT
-- SPDX-FileCopyrightText: 2023 Prokop Randáček <prokop@rdck.dev>
-- SPDX-License-Identifier: CC0-1.0
require('pebble') -- init pebble

-- load a file from github using its pebble string

M example_srht_paste.lua => example_srht_paste.lua +5 -2
@@ 1,4 1,7 @@
-- SPDX-License-Identifier: MIT
require('pebble')
-- SPDX-FileCopyrightText: 2023 Prokop Randáček <prokop@rdck.dev>
-- SPDX-License-Identifier: CC0-1.0
require('pebble') -- init pebble

local pb = require('pastesrht!a03befc656473bdd2af451f30f7e733d43fc7ab3')

print(pb)

M install.sh => install.sh +4 -2
@@ 1,3 1,5 @@
#!/bin/sh
set -e
install -v pebble.lua /usr/share/lua/common/
# SPDX-FileCopyrightText: 2023 Prokop Randáček <prokop@rdck.dev>
# SPDX-License-Identifier: CC0-1.0
set -xe
install pebble.lua /usr/share/lua/common/

M pebble.lua => pebble.lua +28 -35
@@ 1,14 1,15 @@
-- SPDX-FileCopyrightText: 2023 Prokop Randáček <prokop@rdck.dev>
-- SPDX-License-Identifier: MIT
-- https://git.sr.ht/~prokop/lua-pebble
local cache = os.getenv('HOME') .. '/.cache/luapebble'

local cache_path = os.getenv('HOME') .. '/.cache/luapebble'

-- lua5.1 compat
---@diagnostic disable-next-line: deprecated
local unpack, searchers = table.unpack or unpack, package.searchers or package.loaders

local conf = _PEBBLE or {}
local week = (60 * 60 * 24 * 7)
local refetch_time = conf.max_age_seconds or week
local conf = PEBBLE or {}
conf.max_age_seconds = conf.max_age_seconds or 60*60*24*7

local function say(msg) if not conf.quiet then io.write('pebble: ' .. msg .. '\n') end end
local function verbose(msg) if conf.verbose then io.write('pebble: ' .. msg .. '\n') end end


@@ 18,12 19,11 @@ local function run(cmd) -- runs cmd and returns true if retcode is zero plus the
	local r, s, n = os.execute(cmd)
	if s then
		return s == 'exit' and n == 0, cmd -- 5.4
	else
		return r == 0, cmd   -- 5.1
	end
	return r == 0, cmd -- 5.1
end
local function run_stdout(cmd) -- executes cmd and returns its stdout
	verbose("sh: " .. cmd)
	verbose(cmd)
	return io.popen(cmd):read("*a")
end



@@ 39,21 39,15 @@ local function file_older_than(d, t)
end

local function git_clone(dir, url) return run('git clone --quiet --recurse -- "' .. url .. '" "' .. dir .. '"') end
local function git_pull(dir)
	return run('git -C "' .. dir .. '" pull --quiet') and
	    run('git -C "' .. dir .. '" submodule update --init --recursive')
end
local function git_pull(dir) return run('git -C "' .. dir .. '" pull --quiet') and run('git -C "' .. dir .. '" submodule update --init --recursive') end
local function curl(url, file) return run('curl --silent --location "' .. url .. '" -o "' .. file .. '"') end
local function lstar(file, dir) return run_stdout('tar -tf "' .. file .. '"') end
local function untar(file, dir, s)
	return run('tar -xzf "' ..
		file .. '" --directory "' .. dir .. '" --strip-components=' .. (s or 0))
end
local function lstar(file) return run_stdout('tar tf "' .. file .. '"') end
local function untar(file, dir, s) return run('tar xzf "' .. file .. '" --directory "' .. dir .. '" --strip-components=' .. (s or 0)) end

local fetchers = {}

function fetchers.git(url, file, name, max_age)
	local target_dir = cache .. '/' .. name
	local target_dir = cache_path .. '/' .. name
	local target_file = target_dir .. '/' .. file
	local stamp_file = target_dir .. '.timestamp'
	if dir_missing(target_dir) then


@@ 83,10 77,10 @@ function fetchers.git(url, file, name, max_age)
end

function fetchers.targz(url, file, name, max_age)
	local target_dir = cache .. '/' .. name
	local target_dir = cache_path .. '/' .. name
	local target_file = target_dir .. '/' .. file
	local stamp_file = target_dir .. '.timestamp'
	local tar_file = cache .. '/' .. name .. '.tar.gz'
	local tar_file = cache_path .. '/' .. name .. '.tar.gz'
	local fetch = false
	if dir_missing(target_dir) then
		mkdir(target_dir)


@@ 103,8 97,7 @@ function fetchers.targz(url, file, name, max_age)
			return 'pebble: curl failed'
		end

		-- check if all files are inside single directory
		-- and if so, strip the first dir.
		-- check if all files are inside single directory and if so, strip the first dir.
		local files = lstar(tar_file)
		local skip_dirs = 1
		local prefix = string.match(files, "^[^/]+")


@@ 116,9 109,9 @@ function fetchers.targz(url, file, name, max_age)
		end
		if not untar(tar_file, target_dir, skip_dirs) then
			rmdir(target_dir)
			return 'pebble: tar xzf failed'
			return 'pebble: tar failed'
		end
		--rmfile(tar_file)
		rmfile(tar_file)
	end
	local f, err = loadfile(target_file, 't')
	if not f then return err end


@@ 132,10 125,10 @@ function fetchers.targz(url, file, name, max_age)
end

function fetchers.file(url, file, safe_name, max_age)
	if file then say("ignoring unwanted file specifier for file fetcher: '" .. file .. "'") end
	local target_file = cache .. '/' .. safe_name
	if file then say("ignoring extra file specifier for file fetcher: '" .. file .. "'") end
	local target_file = cache_path .. '/' .. safe_name
	if file_missing(target_file) or file_older_than(target_file, max_age) then
		mkdir(cache)
		mkdir(cache_path)
		say('fetching ' .. url)
		if not curl(url, target_file) then
			return "pebble: curl failed"


@@ 150,15 143,15 @@ end

local aliases = {
	-- url format string, fetching function, max age before refetch
	git       = { '%s', fetchers.git, refetch_time },
	srht      = { 'https://git.sr.ht/%s', fetchers.git, refetch_time },
	gh        = { 'https://github.com/%s.git', fetchers.git, refetch_time },
	gist      = { 'https://gist.github.com/%s.git', fetchers.git, math.huge },
	file      = { '%s', fetchers.file, refetch_time },
	pastebin  = { 'https://pastebin.com/raw/%s', fetchers.file, math.huge },
	pastesrht = { 'https://paste.sr.ht/blob/%s', fetchers.file, math.huge },
	targz     = { '%s', fetchers.targz, math.huge },
	ghtag     = { 'https://github.com/%s/archive/refs/tags/%s.tar.gz', fetchers.targz, math.huge },
	git       = { '%s',                                                fetchers.git,   conf.max_age_seconds },
	srht      = { 'https://git.sr.ht/%s',                              fetchers.git,   conf.max_age_seconds },
	gh        = { 'https://github.com/%s.git',                         fetchers.git,   conf.max_age_seconds },
	gist      = { 'https://gist.github.com/%s.git',                    fetchers.git,   math.huge            },
	file      = { '%s',                                                fetchers.file,  conf.max_age_seconds },
	pastebin  = { 'https://pastebin.com/raw/%s',                       fetchers.file,  math.huge            },
	pastesrht = { 'https://paste.sr.ht/blob/%s',                       fetchers.file,  math.huge            },
	targz     = { '%s',                                                fetchers.targz, math.huge            },
	ghtag     = { 'https://github.com/%s/archive/refs/tags/%s.tar.gz', fetchers.targz, math.huge            },
}

table.insert(searchers, function(modname)

M uninstall.sh => uninstall.sh +3 -1
@@ 1,3 1,5 @@
#/usr/bin/env sh
#!/bin/sh
# SPDX-FileCopyrightText: 2023 Prokop Randáček <prokop@rdck.dev>
# SPDX-License-Identifier: CC0-1.0
set -xe
rm /usr/share/lua/common/pebble.lua